Reputation: 231
I am trying to fill 2 different ListView with 2 different groups of data based on its "status". My current code runs with no error but my ListViews are not getting filled. I have multiple test data with both complete or incomplete status.
Java code:
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);
}
Retrieve Functions:
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;
}
Upvotes: 0
Views: 66
Reputation: 8149
Create separate
Adapters
with separatestring list
for bothListViews.
I have created seprate string list and adapter below
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);
Where Condition
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;
}
Upvotes: 1
Reputation: 662
try this
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;
}
Upvotes: 0
Reputation: 168
Why can't you have one function instead of two with an argument?
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;
}
And you can use a CursorLoader instead of this as that is asynchronous
Upvotes: 0