Question

I'm implementing SQLite functionality into my Android project, using this tutorial.

I've got this SQLiteOpenHelper code:

public class HHSSQLiteHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "HHS.db";
    private static final String TABLE_VENDORS = "vendors";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_VENDORID = "vendorId";
    public static final String COLUMN_COMPANYNAME = "companyName";

    public HHSSQLiteHandler(Context context, String vendor,
                       SQLiteDatabase.CursorFactory factory, int company) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }
    . . .

...but trying to instantiate that class this way:

protected void onPostExecute(String result) {
    try {
        JSONArray jsonArr = new JSONArray(result);
        for (int i = 0; i < jsonArr.length(); i++) {
            JSONObject jsonObj = jsonArr.getJSONObject(i);
            String vendorId = jsonObj.getString("vendorId");
            String companyName = jsonObj.getString("companyName");
            // Prepare for writing to db
            Vendor vend = new Vendor();
            vend.setVendorId(vendorId);
            vend.setCompanyName(companyName);
            HHSSQLiteHandler sqliteHandler = new HHSSQLiteHandler(this, null, null, 1);
            sqliteHandler.addVendor(vend);
        }
    } catch (JSONException j) {
        System.out.println(j.getMessage());
        Log.i("jsonEx", j.getMessage());
    }
}

...gives me, "error: incompatible types: MainActivity.GetVendorsTask cannot be converted to Context" on the "HHSSQLiteHandler sqliteHandler = new HHSSQLiteHandler(this, null, null, 1);" line.

What is it expecting, exactly? What do I pass it to appease it?

Was it helpful?

Solution

this in a nested class refers to the nested class instance (GetVendorsTask), not to the outer class instance (MainActivity). Qualify it with e.g. MainActivity.this to refer to the outer class instance, and in case it is a Context (such as an Activity), it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top