1

For some odd reason I can't seem pull off information off my database created in one activity in another. I had the same issues with a static arraylist. Thought it would be much easier using a database but it keeps crashing. My DB instance is always null in the next activity even though I called it before pulling off any information. Here is my function in fragment class

private void populateListView(){ Cursor cursor = myDatabase.getAllData(); String[] fromfieldNames = new String[]{StudentDBOpenHelper.KEY_ID,StudentDBOpenHelper.ITEM_NAME_COLUMN}; int[] toViewIDs = new int[] {R.id.textView_itemNumber,R.id.textView_itemName}; SimpleCursorAdapter myCursorAdapter; myCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.individualview,cursor,fromfieldNames,toViewIDs,0); ListView myList = (ListView) getActivity().findViewById(R.id.courseListXML); myList.setAdapter(myCursorAdapter); }//USed to populate list view 

The function itself is placed outside of onCreateView and onViewCreated but called in onViewCreated.

Here is my database helper class

public class StudentDBOpenHelper extends SQLiteOpenHelper { public static final String KEY_ID = "_id"; public static final String ITEM_NAME_COLUMN = "ITEM_NAME_COLUMN"; public static final String ITEM_CATEGORY_COLUMN = "ITEM_CATEGORY_COLUMN"; public static final String ITEM_GRADE_COLUMN = "ITEM_GRADE_COLUMN"; public static final String DATABASE_NAME = "myItemDatabase.db"; public static final String DATABASE_TABLE = "ItemInfo"; public static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE ="create table " + DATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + ITEM_NAME_COLUMN + " text, " + ITEM_CATEGORY_COLUMN + " text, " +ITEM_GRADE_COLUMN + " integer);"; /* public StudentDBOpenHelper(Context context,String name, SQLiteDatabase.CursorFactory factory,int version) { super(context,name,factory,version); } */ public StudentDBOpenHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL((DATABASE_CREATE)); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w("TaskDBAdapter", "Upgrading from version" + oldVersion + ",which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE); onCreate(db); } public boolean insertData(String firstname,String lastname,String credits){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); //instance of class contentValues.put(ITEM_NAME_COLUMN,firstname);//2 parameters(Column name in which you //you want to insert data // and second is the value itself contentValues.put(ITEM_CATEGORY_COLUMN, lastname); contentValues.put(ITEM_GRADE_COLUMN, credits); long result = db.insert(DATABASE_TABLE,null,contentValues); if(result == -1) { return false; } else { return true; } }//ends insertData functin public Cursor getAllData(){ SQLiteDatabase db = this.getWritableDatabase(); Cursor result = db.rawQuery("select * from " + DATABASE_TABLE,null); return result; }//ends cursor public boolean updateData(String id,String firstname,String lastname,String credits){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(KEY_ID,id); contentValues.put(ITEM_NAME_COLUMN,firstname);//2 parameters(Column name in which you //you want to insert data // and second is the value itself contentValues.put(ITEM_CATEGORY_COLUMN, lastname); contentValues.put(ITEM_GRADE_COLUMN, credits); db.update(DATABASE_TABLE,contentValues,"_id = ?",new String[]{id}); return true; } public Integer deleteData(String id){ SQLiteDatabase db = this.getWritableDatabase(); return db.delete(DATABASE_TABLE,"_id = ?",new String[] {id}); } } 
4
  • give some code of where you initialized the db Commented May 4, 2016 at 15:06
  • Do you actually create a new instance of StudentDBOpenHelper in the next activity before calling any of its methods? Plus, you say 'next activity' but later you speak fragment: so, is it a proper activity or a fragment activity? Commented May 4, 2016 at 15:08
  • I initialized the db inside the Fragment class before the onCreateView and onViewCreated like this StudentDBOpenHelper myDatabase; Commented May 4, 2016 at 15:08
  • My next activity inflates the Fragment Commented May 4, 2016 at 15:10

1 Answer 1

2

The proper way to initialize your database is as following:

StudentDBOpenHelper myDatabase= new StudentDBOpenHelper(this); 

use this as argument if it's an Activity, getContext() if it's a Fragment.

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

8 Comments

I inserted data into the database in my first activity, Would this be the proper way to access the database in the next activity?
@Carlitos a Database in Android is persistent, which means that once data is stored it will remain stored until the user edits/deletes the data (if you implement such method) or until the app is uninstalled or it's data is cleared in Settings. So, creating a new Db instance in another activity is the way to go.
I tried creating a new db instance in the other activity but it keeps saying that the database is empty when I try to access it.
@Carlitos can you write the precise message? Do you see this in logcat or where?
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.carlitos.mycourseapplicationupdate.StudentDBOpenHelper.getAllData()' on a null object reference
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.