0

hello i am building a SQLite Db for my android application . this is the code :

package com.example.pap_e;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class FeedsDbAdapter {

    private final Context mCtx;
    private static final String DEBUG_TAG = "RSSDatabase";
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "rss_data";
    public static String TABLE = "list";
    public static final String ID = "_id";
    public static final String RSS = "_rss";
    public static final String TITLE = "_title";
    public static final String PUBDATE = "_pubdate";
    public static final String DESCRIPTION = "_description";
    public static final String LINK = "_link";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;



    private static class DatabaseHelper extends SQLiteOpenHelper{
        DatabaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE_TABLE"+TABLE+"("+ID+ "integer PRIMARY KEY AUTOINCREMENT,"+RSS+"text NOT NULL,"
                    +TITLE+"text NOT NULL,"+PUBDATE+"text NOT NULL,"+DESCRIPTION+"text NOT NULL,"+LINK+"text NOT NULL"+")");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE);
            onCreate(db);}

    }
    public FeedsDbAdapter open() throws SQLException {

        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
        }
        public void close() {
        mDbHelper.close();
        }

        public boolean updateAnakoinosi(long rowId, String rss, String title,
                String pubdate, String description, String link) {
                ContentValues args = new ContentValues();
                args.put(RSS, rss);
                args.put(TITLE, title);
                args.put(PUBDATE, pubdate);
                args.put(DESCRIPTION, description);
                args.put(LINK, link);
                return mDb.update(TABLE, args, ID + "=" + rowId, null) > 0;}
                public Cursor fetchAllAnakoinoseis() {
                return mDb.query(TABLE, new String[] { ID, RSS, TITLE, PUBDATE,
                DESCRIPTION,LINK }, null, null, null, null, null); }



}

The thing is that i get an error at public class FeedsDbAdapter that says:"The blank final field mCtx may not have been initialized" but i had it initialized using private final Context mCtx; Am i missing something here ? Thanks a lot in advance!

4 Answers 4

1

You didn't initialize the context yet. You have to initialize it inside FeedsDbAdapter constructor like :

public FeedsDbAdapter (Context context){

       mCtx = context;
     }

first declare FeedsDbAdapter constructor, because you use FeedsDbAdapter class through its constructor in other classes ans the other activity context will assign to this current context.

Sign up to request clarification or add additional context in comments.

1 Comment

glad to help you ..all the best.
1

Change your code to :

private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);

        mCtx = context;
    }

1 Comment

I did what you told me to do but the error wasn't fixed.Also it displayed an error at mCtx = context; that says:"Cannot make a static reference to the non-static field mCtx"
0

This question of yours relates to java. This line private final Context mCtx; is just a declaration of field mCtx of type context. And as this field has been declared as final, It has to be iniliazed inside the constructor mCtx = context;. Even if mCtx is not declared as final, it has still to be initialized before being used.

Comments

0

Thats because your class is static... Remove static from your class and change your code to

DatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    mCtx = context;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.