android - 无法在 Android 中使用 rawQuery 命令填充 ListView

标签 android sqlite android-listview

我在使用 rawQuery 命令在 Android 中填充 ListView 时遇到了一些问题,我希望对它有所帮助。

我正在使用一种方法从 Assets 文件夹复制我自己的数据库,我相信它工作正常。数据库正在被正确复制,因为我在 DDMS 中看到了这种情况。我还收到了 log.i 条目,这些条目告诉我数据库也正在正确打开。我在 Logcat 时没有收到任何错误,但没有任何内容填充我的 ListView

我下面查询数据库并报告的命令有问题吗?

如有任何帮助,我们将不胜感激。

        private void fillData() {
        Cursor c =  myDbHelper.rawQuery("SELECT name FROM sqlite_master",   null);
            startManagingCursor(c);
        // Create an array to specify the fields we want to display in the list.
            String[] from = new String[]{"name"};

            // an array of the views that we want to bind those fields too.
            int[] to = new int[]{R.id.listview};

            // Now create a simple cursor adapter and set it to display
            SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.list_view, c, from, to);
            setListAdapter(notes);             
      }

这是我的 lists.xml 文件,我希望在其中填充 ListView 的结果:

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

      <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
         <TextView
            android:text = "Lists"
            style="@style/HeaderText"
            android:textSize="15dp"
            android:gravity="center" />
          <View
            android:layout_width="wrap_content"
            android:background="@drawable/gradient"
            android:layout_height="1dp" />
        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <EditText
                android:id="@+id/searchText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

        <Button
                android:id="@+id/searchButton"
                android:text="Search"
                android:background = "@drawable/main_buttons"
                style="@style/ButtonText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />"

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

             <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"/>
        </LinearLayout>

        </LinearLayout>

最后,list_view 布局只是一个 TextView,用于从 rawQuery 命令填充正确的列表。

最佳答案

如果你想在你的列表中显示表名,你可以试试这个 fillData() 方法:

private void fillData() {
        Cursor c = myDbHelper.rawQuery("SELECT name FROM sqlite_master", null);
        ArrayList<String> values = new ArrayList<String>();
        while (c.moveToNext()) {
            values.add(c.getString(c.getColumnIndex("name")));
        }
        c.close();
        // Now create a simple cursor adapter and set it to display
        ArrayAdapter<String> notes = new ArrayAdapter<String>(this,
                R.layout.list_view, values);
        setListAdapter(notes);
    }

ListView 要求其所基于的游标具有_id 列。但是在该表中,您没有要添加到查询中的 _id,因此您可以尝试将游标从原始查询解析为 ArrayList,然后将其绑定(bind)到一个简单的 ArrayAdapter

关于android - 无法在 Android 中使用 rawQuery 命令填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387551/

相关文章:

php - 带有 PHP 和 SQLite 的数据库应用程序在数据库中返回空值

android - 在android中实现一个带有分区的列表

android - 在什么情况下应用程序内结算版本 3 服务器更改可在客户端设备上使用?

sqlite - 在 SQLite 数据库浏览器中创建外键

java - 如何在某个屏幕后关闭某些 Activity ?

python - 在存在条件的数据库中查找非唯一值

android - 如何判断 ListView 何时有 subview ?

android - 带有自定义 ArrayAdapter : Works fine, 的 ListView 直到我需要更改项目数?

java - Android.widget.RelativeLayout 无法转换为 android.widget.ImageView

android - 使用 MPAndroidChart 库显示水平条形图,如下图所示