使用不同的行布局填充单个 ListView

发布于 2024-11-05 05:21:40 字数 1246 浏览 1 评论 0原文

我正在尝试使用数据库中的列填充 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-11-12 05:21:40

当然,只会显示最后一项。这是因为每次进入循环并执行时都会不断更改列表适配器。摆脱循环。

如果我理解正确,您想要显示绿色行或不显示绿色行,具体取决于待办事项。如果是这样,请编写您自己的列表适配器,它是 SimpleCursorAdapter 的子类。然后重写 newView (Context context, Cursor 光标, ViewGroup Parent) 方法来选择用于显示行的视图。 cursor 参数已移动到正确的位置,因此您可以从数据库中检索值以进行任何必要的比较。

例如:我假设(因为 fetchTodo(intarray[x]))您正在根据 ToDo 的 id 进行比较。在这种情况下,您的代码可能如下所示:

 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);
      }
 }

我不知道我是否很好地理解了您的代码,但您可以对其进行调整以满足您的需求。

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.

红颜悴 2024-11-12 05:21:40

很可能在每一行中使用不同的视图:但是您不能为其使用标准的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文