【问题标题】:Can I add data from different tables of the same database into each row of a ListView in Android?我可以将来自同一数据库的不同表的数据添加到 Android 中 ListView 的每一行中吗?
【发布时间】:2011-02-07 20:38:24
【问题描述】:

我可以将同一数据库的不同表中的数据添加到 Android 中 ListView 的每一行吗?

我有一个供应商应用程序,我想将一个标准项目列表表和一个每日表中的数据添加到列表视图中。

【问题讨论】:

  • 请注意:您不必使用Cursor/CursorAdapter 来填充ListView。您可以使用来自任何来源(多个表、字符串资源等)的对象填充您自己的自定义列表/数组
  • 如何在没有游标的情况下从数据库中填充 listView?

标签: android sqlite listview


【解决方案1】:

如果两个表具有相同的行格式(或者至少您要从两个表中选择相同类型的行),您可以使用 UNION 将它们组合成一个查询。有关 UNION 的更多信息,请参阅sqlite select query documentation

【讨论】:

    【解决方案2】:

    我认为您的意思是查询连接了两个不同的表;对吗?

    如果您使用的是SimpleCursorAdapter,那么您可以使用CursorToStringConverter 为ListView 提供标签。这是一个例子:

        // choices to be displayed in the AutoCompleteTextView.
        adapter.setCursorToStringConverter(new CursorToStringConverter() {
            public String convertToString(android.database.Cursor cursor) {
                final int columnIndex1 = cursor.getColumnIndexOrThrow("col1");
                final String str1 = cursor.getString(columnIndex1);
                final int columnIndex2 = cursor.getColumnIndexOrThrow("col2");
                final String str2 = cursor.getString(columnIndex2);
                return str1 + str2;
            }
        });
    

    如果您希望将每个表中的数据显示在单独的视图中(而不是在单个 TextView 中),那么您可以使用SimpleCursorAdapter.ViewBinder 来更新视图。 (ViewBinder 的Here's an example。我写这个是为了与 Spinner 一起工作,但它与 ListView 的工作方式相同。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 2011-03-07
      • 2021-03-28
      相关资源
      最近更新 更多