I am attempting to populate a ListView with a column from a database. I can achieve this successfully, but the problem is i would like to use a different layout resource depending on if the id matches a PendingIntent.
This is to check if an alarm exists with the rowid, and i plan to have visual feedback in the listview to show the user which alarms exist. I can successfully check the rowid against PendingIntents and get a return boolean.
The issue I run into is populating the ListView with different layout resources for different rows (is this even possible?) and i 开发者_JS百科have no idea where to go from here.
I currently have the following code:
for(int x = 0; x < numNotes; x++) {
if(existAlarm(intarray[x])){
String[] from = new String[] { TodoDbAdapter.KEY_SUMMARY };
int[] to = new int[] { R.id.label1};
cursor = dbHelper.fetchTodo(intarray[x]);
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.todo_row_green, cursor, from, to);
setListAdapter(notes);
} else if (!existAlarm(intarray[x])){
String[] from = new String[] { TodoDbAdapter.KEY_SUMMARY };
int[] to = new int[] { R.id.label};
cursor = dbHelper.fetchTodo(intarray[x]);
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.todo_row, cursor, from, to);
setListAdapter(notes);
}
}
But the ListView just shows the final database entry from the loop.
I would be very grateful for any guidance, Thanks.
Of course, only the last item will be shown. This is because you keep changing the list adapter each time you enter the loop and execute. Get rid of the loop.
If I understand correctly, you want to display a either a green row or not depending on the ToDo. If so, write your own list adapter which is a subclass of the SimpleCursorAdapter. Then override the newView (Context context, Cursor cursor, ViewGroup parent)
method to choose view to use to display the row. The cursor
parameter is already moved to the correct position, so you can retrieve values from the DB for any necessary comparisons.
For example: I assume (because of the fetchTodo(intarray[x])) that you are comparing based on the id of the ToDo. In that case your code may look like:
public View newView (Context context, Cursor cursor, ViewGroup parent){
long id = cursor.getLong(TodoDbAdapter.ROW_ID_COLUMN);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (existAlarm(id)){
return inflater.inflate(R.layout.todo_row_green, null);
} else {
return inflater.inflate(R.layout.todo_row, null);
}
}
I don't know if I understood your code well, but you can adapt this to suit your needs.
Its quite possible to use a different view in each row: you however can't use a standard SimpleCursorAdapter for it: you have to overload the getView() function. An example is at Trying to override getView in a SimpleCursorAdapter gives NullPointerExceptio (there are a couple of errors which are answered in the post, but the basic technique is correct).
As far as showing only the last post, your code is creating a NEW SimpleCursorAdapter for every row, which is incorrect. You want to use the getView() override and then do the for loop and comparison in there.
精彩评论