gpt4 book ai didi

android - 只需在 Android ListView 中填充数据库表名

转载 作者:太空狗 更新时间:2023-10-29 15:19:33 25 4
gpt4 key购买 nike

Android 开发的新手...我一直在互联网上寻找答案,但我仍然发现自己被困住了,希望有人能帮我一把。我绝对承认我是新手...但正在努力学习。

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

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

我的 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 调用上很努力......但我被卡住了。

最佳答案

您的 to 和 from 数组不正确。您的“来自”字符串数组当前为空。这将是您要显示的列的名称。所以在你的情况下它将是“名字”。您的“to”数组将成为您将值映射到的 View 的 ID...而不是列表的 ID。您将需要创建一个新布局,其中包含要显示的 TextField。在“收件人”字段中,您将给出这个新文本字段的 ID。然后在 SimpleCursorAdapter 中,您可以发送这个新布局以及“to”和“from”数组。

关于android - 只需在 Android ListView 中填充数据库表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9345939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com