Android:从数据库填充ListView

时间:2015-01-14 02:16:03

标签: android sqlite listview

我有一个SQLite数据库,我想用它来填充ListView,但我不能为我的生活弄清楚如何做到这一点。基于各种互联网资源(其他问题/教程/等),我已经能够获得这些代码,但它显然无法正常工作,因为我现在甚至无法在模拟器中打开应用程序:

populateListView方法

public void populateListView() {
    Cursor cursor = db.getAllContacts();
    String[] fromFieldNames = new String[] {DBAdapter.KEY_ROWID, DBAdapter.KEY_LOCATION, DBAdapter.KEY_TIME};
    int[] toViewIds = new int[] {R.id.textView3, R.id.textView2, R.id.textView4};
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
            R.layout.list_view_layout, cursor, fromFieldNames, toViewIds, 0);

    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(myCursorAdapter);
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/TableLayout">


<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Set New Alarm"
    android:id="@+id/setAlarmButton"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="onClickSetAlarm" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView" />

</RelativeLayout>

list_view_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView3" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView4"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

我很确定问题不在于DBAdapter的代码,因为它在我添加这段代码之前工作正常(我让它显示了Toast with the data)。

谢谢!

2 个答案:

答案 0 :(得分:1)

使用 SimpleCursorAdapter

Cursor cursor = obj.fetchAllNames();
        registerForContextMenu(lv);

        String[] columns = new String[] {
                obj.KEY_MAIL,
                obj.KEY_NAME

              };


          // the XML defined views which the data will be bound to
          int[] to = new int[] {
            R.id.tvname,
            R.id.tvemail

          };

          // create the adapter using the cursor pointing to the desired data
          //as well as the layout information
          try{
          dataAdapter = new SimpleCursorAdapter(
            this, R.layout.viewusertext,cursor,columns,
            to,
            0);
          }catch(Exception e)
          {e.printStackTrace();}

          lv.setAdapter(dataAdapter);

其中lv是listview,dataAdapter是 private SimpleCursorAdapter dataAdapter;

用于检索要在列表中显示的所需元素的查询:

public Cursor fetchAllNames() {

          Cursor mCursor = app.myDbHelper.MyDB().query("reg", new String[] {"email as _id","fname"},
            null, null, null, null, null);
          try{

          if (mCursor != null) {
           mCursor.moveToFirst();
          }
          return mCursor;
          }catch(Exception e)
          {
              return mCursor;
          }
         }

&#34; reg&#34; 是表名。

注意:使用simplecursoradapter时,您应该使用主键_id。在我的情况下,电子邮件是主键,我将其定义为公共静态最终字符串KEY_MAIL =&#34; _id&#34 ;;

答案 1 :(得分:0)

这个问题我昨天才回答。最简单的创建CursorLoader的方法,它只会在活动创建时从DB加载数据,并且很容易设置适配器:只需要形成列&#34;来自&#34; (DB.COLUMN_NAME)和&#34;到&#34; (R.id.textviewToDisplay)。 请检查这个答案。 retriveing data from exisint sqlite database