繁体   English   中英

Android:从数据库填充aa ListView

[英]Android: Populate a a ListView from a Database

我有一个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)。

谢谢!

使用一个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是私有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;
          }
         }

“ reg”是表名。

注意:使用simplecursoradapter时,应将主键用作_id 。在我的情况下,电子邮件是主键,并且我已将其定义为公共静态最终字符串KEY_MAIL =“ _ id”;

我昨天才回答这个问题。 创建CursorLoader的最简单方法是,仅在创建活动并易于设置适配器时才从DB后台在数据库中加载数据一次,并且只需设置“ from”( DB.COLUMN_NAME )和“ to”( R.id.textviewToDisplay )列即可。 请检查此答案。 从现有的sqlite数据库中检索数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM