【问题标题】:populating custom listview from SQLite database从 SQLite 数据库填充自定义列表视图
【发布时间】:2011-02-07 09:25:02
【问题描述】:

我在 onCreate() 中有这段代码:

try{
    Log.i("onCreate","Trying to open database...");
    orgi = this.openOrCreateDatabase("orgi", MODE_PRIVATE, null);

    Log.i("onCreate","database opened.");
    cursor = orgi.rawQuery("SELECT name from profile;",null);
    Log.i("onCreate","successfully done with query..");
    adapter = new SimpleCursorAdapter(this,R.layout.profilelistrow,cursor, 
            new String[]{"name"},new int[] {R.id.profileListRowTV1});
    Log.i("onCreate","Adapter done");

    ll.setAdapter(adapter);

}catch (SQLException e){
    displayError(e.toString());
}

我应该使用 SQLite 数据库中的数据填充 customlistview。但它在这条线上给了我一种力量:

adapter = new SimpleCursorAdapter(this,R.layout.profilelistrow,cursor, 
            new String[]{"name"},new int[] {R.id.profileListRowTV1});

谁能告诉我怎么了?我对简单的光标适配器一无所知(由于我在 android 中的菜鸟)... R.layout.profilelistrow 是我的整个屏幕布局,而 profileListRowTV1 是将插入“名称”的文本视图。请教育我...我做错了什么?

【问题讨论】:

  • 请发布异常的堆栈跟踪。

标签: android sqlite listview


【解决方案1】:

我不知道是什么问题,因为你给了我们代码并且没有错误,在 eclipse -> Window -> Open Perspective -> DDMS。当你用力关闭时,看看日志猫。无论如何。这是我朋友的一个例子。

主列表 XML...您可以在布局中的任何位置添加列表视图...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFD0"
android:layout_height="fill_parent"
android:orientation="vertical">



 <ListView  
     android:id="@android:id/list"
     android:cacheColorHint="#666666"
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     />

 <TextView android:id="@android:id/empty"
     android:layout_width="wrap_content"
     android:textColor="#4B89E0" 
     android:layout_height="wrap_content"
     android:text="The list is empty! Do I need this? No idea!"
     />


</LinearLayout>

下一行: 现在这些可能会变得非常复杂。我有一个包含 5 个图像和 3 个文本视图的列表的程序,android 似乎可以很好地处理它。但这是一个基本的,3 个文本视图...也可以与一个一起使用。

<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:paddingTop="4dip"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:paddingBottom="6dip"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical">

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

 <TextView android:id="@+id/text1"
    android:textColor="#4B89E0" 
     android:layout_width="130dip"
     android:layout_height="40dip"
     android:textSize = "15dip"
     />

 <TextView android:id="@+id/text2"
    android:textColor="#4B89E0" 
     android:layout_width="70dip"
     android:gravity = "center"
     android:textStyle = "bold"
     android:layout_height="wrap_content" />

 <TextView android:id="@+id/text3"
    android:textColor="#4B89E0" 
    android:gravity = "center"
     android:layout_width="80dip"
     android:layout_height="wrap_content"  />

</LinearLayout>          

</LinearLayout>

最后是一些代码把它放在一起......在这个例子中调用fillData();填写清单。我通常喜欢这样做,这样当我更改数据库时,我正在检索哪些信息(即,我想查看:所有形状或圆形或方形)列表很容易更新。我假设您的数据库已经打开。您在 OnCreate() 中打开了它。 与其在 OnCreate 方法中创建所有这些代码,不如创建自己的方法“fillData”,而您在 OnCreate 中所拥有的只是 fillData();。这将使查看、编辑和故障排除变得更加容易。

private void fillData() {

        Cursor fetchInfo = mDbHelper.fetchAllRecords();
        startManagingCursor(fetchInfo);


        // Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER)
        String[] from = new String[]{DbAdapter.KEY_TITLE, 
                 DbAdapter.KEY_DATE, DbAdapter.KEY_AUTHOR   };

        // an array of the views that we want to bind those fields to (in this case text1,text2,text3)
        int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3};

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

WakaWaka

祝你好运

【讨论】:

    【解决方案2】:

    你的问题应该在这一行。

    cursor = orgi.rawQuery("SELECT name from profile;",null);
    

    SimpleCursorAdapter 以及许多 Android 函数都依赖于一个名为“_id”的标识符键。因此,请确保您的查询返回具有该标识符的行。在您的情况下,您应该将该行更改为此

    cursor = orgi.rawQuery("SELECT yourTableId AS '_id', name FROM profile;",null);
    

    这应该可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      相关资源
      最近更新 更多