【问题标题】:Simply populate database table names in Android ListView只需在 Android ListView 中填充数据库表名
【发布时间】:2012-03-09 21:31:37
【问题描述】:

对 Android 开发非常陌生...我一直在互联网上寻找答案,但我仍然发现自己陷入困境并希望有人能帮帮我。我绝对承认我是一个新手……但我正在努力学习。

我有一个已经包含在一个包中的 sqlite 数据库。我已将其复制到资产文件夹中。该数据库有 4 个表(以及下面的一些字段)。

我有一个简单的列表视图活动,我试图简单地在数据库中查询表名并填充它们,但是我没有得到任何结果。

我的 DataBaseAdapter 代码本质上是这里的代码: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

我修改了我的数据库名称和路径。

我的班级代码在这里:

编辑:::

public Cursor fetchAllNotes() {

    return myDbHelper.rawQuery("SELECT name FROM sqlite_master WHERE type='table'",
            new String[]{});
}

private void fillData() {

    Cursor c = fetchAllNotes();
    startManagingCursor(c);

    String[] from = new String[] {"name"};
    int[] to = new int[] {R.id.listview};


    SimpleCursorAdapter notes = 
            new SimpleCursorAdapter (this, R.layout.list_view, c, from, to);
    setListAdapter(notes);

            }

    }

项目运行,它只是在 ListView(在名为 lists.xml 的 xml 中)中没有产生任何结果,代码部分在这里:

        <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />    

         <TextView android:id="@android:id/empty"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#000000"
           android:text="No data"
           style="@style/ButtonText"/>

任何帮助都会很好,我确信我在我的 sql 调用上很努力......但我被卡住了。

【问题讨论】:

    标签: android database sqlite listview


    【解决方案1】:

    您的 to 和 from 数组不正确。您的“来自”字符串数组当前为空白。这将是您尝试显示的列的名称。因此,在您的情况下,它将是“名称”。您的 'to' 数组将是您将值映射到的视图的 id...而不是列表的 id。您将需要创建一个新布局,其中包含要显示的 TextField。在“to”字段中,您将给出这个新文本字段的 id。然后在 SimpleCursorAdapter 中,您可以将这个新布局连同 'to' 和 'from' 数组一起发送。

    【讨论】:

    • 尼克,我非常感谢您对此的回应。我在 ID 为 listview 的 TextView 中创建了一个名为“list_view”的新布局文件。我编辑了上面的代码片段以显示我的更改。当我启动它时,我仍然看到活动中没有填充任何内容。只是为了确认,当我通过 DDMS 查看应用程序数据时,数据库确实存在于应用程序数据中。你能想到我可能缺少的其他东西吗?
    【解决方案2】:

    试试这个代码:

    private void getData() {
    
                tb.open();
    
                List<String> items = new ArrayList<String>();
    
                 Cursor c = tb.fetchallemployee();
                 c.moveToFirst(); 
    
                 ListAdapter adapter=new SimpleCursorAdapter(this,
                                 R.layout.show_database, c,
                                 new String[] {"date", "title"},
                                 new int[] {R.id.toptext, R.id.bottomtext});
                 setListAdapter(adapter);
    
                 setListAdapter(entries);
    
                 tb.close(); 
            }
    

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多