我正在尝试根据其“状态”用2个不同的数据组填充2个不同的ListView。我当前的代码运行没有错误,但我的ListViews没有得到填充。我有多个完整或不完整状态的测试数据。
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<String> dBaseContent = dBase2.retrieveIncompleteRows();
ArrayAdapter<String> 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<String> retrieveIncompleteRows() {
ArrayList<String> 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<String> retrieveCompleteRows() {
ArrayList<String> 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;
}
答案 0 :(得分:1)
为
创建单独的Adapters
string list
ListViews.
我在
下创建了单独的字符串列表和适配器 ArrayList<String> dBaseContent = dBase2.retrieveIncompleteRows();
ArrayAdapter<String> arrayAdpt = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, dBaseContent);
taskList.setAdapter(arrayAdpt);
ArrayList<String> dBaseContentComplete = dBase2.retrieveCompleteRows();
ArrayAdapter<String> arrayAdptComplete = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dBaseContentComplete);
taskList2.setAdapter(arrayAdptComplete);
条件
public ArrayList<String> retrieveIncompleteRows() {
ArrayList<String> studentRows = new ArrayList<>();
String[] columns = new String[] {"tid", "task", "status"};
String s = "Incomplete";
//make changes here... and similarly while getting complete records other funtion.
Cursor cursor = db.query(DB_TABLE, columns,
"status = '"+ s + "'", null, 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;
}
答案 1 :(得分:0)
为什么你不能用一个函数代替两个函数?
public ArrayList<String> retrieveCompleteRows(String s) {
ArrayList<String> studentRows = new ArrayList<>();
String[] columns = new String[] {"tid", "task", "status"};
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;
}
你可以使用CursorLoader代替这个,因为它是异步的
答案 2 :(得分:0)
试试这个
public ArrayList<String> retrieveIncompleteRows() {
ArrayList<String> studentRows = new ArrayList<>();
String s = "Incomplete";
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " = " + s + "";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
studentRows.add(cursor.getString(0) + ". " + cursor.getString(1));
} while (cursor.moveToNext());
}
return studentRows;
}