使用不同的行布局填充单个 ListView
我正在尝试使用数据库中的列填充 ListView。我可以成功实现这一点,但问题是我想根据 id 是否与 PendingIntent 匹配来使用不同的布局资源。
这是为了检查 rowid 是否存在警报,我计划在列表视图中提供视觉反馈,以向用户显示存在哪些警报。我可以成功地根据 PendingIntents 检查 rowid 并获得返回布尔值。
我遇到的问题是为不同的行使用不同的布局资源填充 ListView (这可能吗?),我不知道从这里到哪里去。
我目前有以下代码:
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);
}
}
但 ListView 仅显示循环中的最终数据库条目。
我将非常感谢任何指导,谢谢。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,只会显示最后一项。这是因为每次进入循环并执行时都会不断更改列表适配器。摆脱循环。
如果我理解正确,您想要显示绿色行或不显示绿色行,具体取决于待办事项。如果是这样,请编写您自己的列表适配器,它是 SimpleCursorAdapter 的子类。然后重写
newView (Context context, Cursor 光标, ViewGroup Parent)
方法来选择用于显示行的视图。cursor
参数已移动到正确的位置,因此您可以从数据库中检索值以进行任何必要的比较。例如:我假设(因为 fetchTodo(intarray[x]))您正在根据 ToDo 的 id 进行比较。在这种情况下,您的代码可能如下所示:
我不知道我是否很好地理解了您的代码,但您可以对其进行调整以满足您的需求。
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. Thecursor
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:
I don't know if I understood your code well, but you can adapt this to suit your needs.
很可能在每一行中使用不同的视图:但是您不能为其使用标准的 SimpleCursorAdapter:您必须重载 getView() 函数。一个示例位于 尝试在 SimpleCursorAdapter 中覆盖 getView 会给出 NullPointerException< /a> (帖子中回答了一些错误,但基本技术是正确的)。
就仅显示最后一篇文章而言,您的代码正在为每一行创建一个新的 SimpleCursorAdapter,这是不正确的。您想要使用 getView() 覆盖,然后在其中执行 for 循环和比较。
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.