我想根据其“状态”用2个不同的数据组填充2个不同的ListView。我目前的代码运行时没有错误,但我的ListView没有被填充。我有多个完整或不完整状态的测试数据。Android - 如何检索一个WHERE条件的数据库行?
Java代码:
public class view_to_do_task extends AppCompatActivity {
private dbasemanager2 dBase2;
private ListView taskList;
private ListView taskList2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_to_do_task);
dBase2 = new dbasemanager2(this);
dBase2.openReadable();
taskList = (ListView)findViewById(R.id.incompleteTaskList);
ArrayList dBaseContent = dBase2.retrieveIncompleteRows();
ArrayAdapter arrayAdpt = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, dBaseContent);
taskList.setAdapter(arrayAdpt);
taskList2 = (ListView)findViewById(R.id.completeTaskList);
dBaseContent = dBase2.retrieveCompleteRows();
arrayAdpt = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dBaseContent);
taskList.setAdapter(arrayAdpt);
}
找回功能:
public ArrayList retrieveIncompleteRows() {
ArrayList studentRows = new ArrayList<>();
String[] columns = new String[] {"tid", "task", "status"};
String s = "Incomplete";
Cursor cursor = db.query(DB_TABLE, columns, "status=?", new String[] { s }, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
studentRows.add(cursor.getString(0) + ". " + cursor.getString(1));
cursor.moveToNext();
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return studentRows;
}
public ArrayList retrieveCompleteRows() {
ArrayList studentRows = new ArrayList<>();
String[] columns = new String[] {"tid", "task", "status"};
String s = "Complete";
Cursor cursor = db.query(DB_TABLE, columns, "status=?", new String[] { s }, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
studentRows.add(cursor.getString(0) + ". " + cursor.getString(1));
cursor.moveToNext();
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return studentRows;
}