Answer the question
In order to leave comments, you need to log in
ListView is not populated from database with SimpleCursorAdapter. What to do?
I'm making an android notepad to practice with listview, adapters and SQLite. When using SimpleCursorAdapter, this error occurs:
"Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such table: NOTE_TABLE (code 1): , while compiling: INSERT INTO NOTE_TABLE (note_text) VALUES ( 'TEXT');)"
Tell me what the problem is.
Below is the error and class code.
public class ListActivity extends AppCompatActivity {
ListView notelistView;
SimpleCursorAdapter cursorAdapter;
SQLiteDatabase db;
SQLiteOpenHelper dbhelp;
DatabaseHelper helper;
Cursor listCursor;
String[] from;
int[] to;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ListView noteList = findViewById(R.id.note_list);
dbhelp = new DatabaseHelper(this);
db = dbhelp.getReadableDatabase();
helper = new DatabaseHelper(this);
from = new String[] {helper.NOTE_TEXT};
to = new int[] {R.id.note_list};
db.rawQuery("INSERT INTO NOTE_TABLE (note_text) VALUES ('TEXT');", null);
listCursor = db.rawQuery("SELECT note_first_string FROM note_table",null); //ORDER BY _id
cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, listCursor, from, to, 0);
noteList.setAdapter(cursorAdapter);
}
}
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "NOTE_DB";
public static int DATABASE_VERSION = 1;
public static String TABLE_NAME = "note_table";
public static final String NOTE_TEXT = "note_text";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.rawQuery("CREATE TABLE NOTE_TABLE (_id INTEGER PRIMARY KEY AUTOINCREMENT," + " note_text TEXT," + " note_first_string TEXT);", null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question