I am working from a fragment java class, and I want to change the listview of a fragment to contain some items from a SQLite database.
Here is the code for the populating:
private void populateListView() {
Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[] {DBAdapter.KEY_VAK, DBAdapter.KEY_LOKAAL};
int[] toViewIDs = new int[] {R.id.textVak, R.id.textLokaal};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.layout, cursor, fromFieldNames, toViewIDs, 0);
ListView myList = (ListView) getView().findViewById(R.id.listView);
myList.setAdapter(myCursorAdapter);
}
And here is the code for the DBAdapter:
public class DBAdapter {
private static final String TAG = "DBAdapter"; //used for logging database version changes
// Field Names:
public static final String KEY_ROWID = "_id";
//public static final String KEY_DAG = "dag";
//public static final String KEY_UUR = "uur";
public static final String KEY_VAK = "vak";
public static final String KEY_LOKAAL = "lokaal";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID,KEY_VAK, KEY_LOKAAL}; // KEY_DAG, KEY_UUR
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
//public static final int COL_DAG = 1;
//public static final int COL_UUR = 2;
public static final int COL_VAK = 1;
public static final int COL_LOKAAL = 2;
// DataBase info:
public static final String DATABASE_NAME = "db";
public static final String DATABASE_TABLE = "Rooster";
public static final int DATABASE_VERSION = 1; // The version number must be incremented each time a change to DB structure occurs.
//SQL statement to create database
private static final String DATABASE_CREATE_SQL = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_VAK + " TEXT, " + KEY_LOKAAL + " TEXT" + ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new set of values to be inserted into the database.
public long insertRow(String task, String date) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_VAK, task);
initialValues.put(KEY_LOKAAL, date);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Change an existing row to be equal to new data.
//public boolean updateRow(long rowId, String vak, String lokaal) {
// String where = KEY_ROWID + "=" + rowId;
// ContentValues newValues = new ContentValues();
// newValues.put(KEY_VAK, vak);
// newValues.put(KEY_LOKAAL, lokaal);
// // Insert it into the database.
// return db.update(DATABASE_TABLE, newValues, where, null) != 0;
//}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
}
The error I get is as follows:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
Aucun commentaire:
Enregistrer un commentaire