![](/img/trans.png)
[英]How to add data into SQLite database from recycler view in android studio
[英]How to add data from an SQLite Database into an ArrayList (Android)
我是Android的新手,正在制作待辦事項列表應用程序。 我有一個包含一列任務的數據庫。 如何將這些任務放到ArrayList中並顯示它?
helper = new TaskDBHelper(MainActivity.this);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query(TaskContract.TABLE, new String[]{TaskContract.Columns._ID,TaskContract.Columns.TASK}, null, null, null, null, null);
arrayListToDo = new ArrayList<String>();
arrayAdapterToDo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListToDo);
ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
listViewToDo.setAdapter(arrayAdapterToDo);
試試這個代碼
while (cursor.moveToNext()) {
arrayListToDo.add(cursor.getString(cursor.getColumnIndex("Your column name")));
}
您可以在數組列表中獲取數據:
ArrayList<String> arrayListToDo= new ArrayList<String>(); //your query will returns cursor if (cursor.moveToFirst()) { do { arrayListToDo.add(get you string from cursor); } while (cursor.moveToNext()); }
現在將這個列表添加到您的適配器中。
從數據庫獲取列表數據,這只是概述
ArrayList<String> data = databaseObject.GetList();
ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
arrayAdapterToDo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listViewToDo.setAdapter(arrayAdapterToDo);
ArrayList<String> GetList()
{
ArrayList<String> arrayListtemp= new ArrayList<String>();
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query(TaskContract.TABLE, new String[]{TaskContract.Columns._ID,TaskContract.Columns.TASK}, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
arrayListtemp.add("");// added your table value from database
} while (cursor.moveToNext());
}
return arrayListtemp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.