The constructor DbHelper(CustomListPreference) is undefined

443 views Asked by At

Please let me know what is wrong with the syntax. I get error 'The constructor DbHelper(CustomListPreference) is undefined' Here my code:

CustomListPreference.java

public class CustomListPreference extends ListPreference {
    CustomListPreferenceAdapter customListPreferenceAdapter = null;
    Context mContext;

    private SQLiteDatabase db;
    DbHelper dbHelp = new DbHelper(this); (Here I get an error)
...

DbHelper.java

    import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {
    static String DATABASE_NAME="myBase";
    public static final String KEY_NAME="name";
    public static final String KEY_ID="id";

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
         db.execSQL("create table tUser ("
                  + "id integer primary key autoincrement," 
                  + "name text,"
                  + "exists integer" + ");");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);

    }

}
1

There are 1 answers

3
Madhur Ahuja On BEST ANSWER

You are passing ListPreference object whereas your DBHelper expects Context. See this line:

public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

ListPreference is not a child class of Context and hence this error. You will have revisit your design.

Update:

You should change this line to:

DbHelper dbHelp = new DbHelper(mContext);