sander126
sander126

Reputation: 55

How to instantiate a SQLiteOpenHelper Class on a ListFragment?

Hi I have a class that extends SQLiteOpenHelper named "KnowledgeBaseHelper". I've been using it for a while with my Classes that extends Activity, but then I decided to shift from using ListActivity to ListFragment, the problem is, when I tried to instantiate my class named KnowledgeBaseHelper like this "KnowledgeBaseHelper helper = new KnowledgeBaseHelper(ListActivity.this);" it gives me an error of "The constructor KnowledgeBaseHelper(ListActivity) is undefined"

This usually works when I'm extendsing ListActivity but now I'm stock with this error. Does the instantiation differ from ListActivity and FragmentActivity?

Thanks

Below are my codes

public class ListActivity extends ListFragment {
    ArrayList<String> symptomList;
    String name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_generalsymptoms, container,
            false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        new LoadSymptoms().execute();
    }

    class LoadSymptoms extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
            KnowledgeBaseHelper helper = new KnowledgeBaseHelper(
                ListActivity.this);

            return null;
    }
}

For the SQLiteOpenHelper, I have a very long code and methods so I decided to include only few.

public class KnowledgeBaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "docdroid";

    //... I deleted the following lines        

    public KnowledgeBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        // Create Clinic Table
        createTable(db, TABLE_CLINIC, CLINIC_COLUMN);
        //... I deleted the following lines
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CLINIC);
        onCreate(db);
        //... I deleted the following lines
    }
}

Is there other way to instantiate the class that extends SQLiteOpenHelper in Fragments?

Upvotes: 2

Views: 1117

Answers (1)

laalto
laalto

Reputation: 152847

The argument you need to pass to KnowledgeBaseHelper constructor is a Context.

An Activity is-a Context so you can use this in an activity.

A Fragment isn't a Context so you cannot use this in a fragment.

You can use getActivity() in a fragment to get a reference to the hosting activity (which is -a Context).

Upvotes: 2

Related Questions