2015-01-14 112 views
0

我有一个SQLite数据库,我想用来填充一个ListView,但我不能为我的生活弄清楚如何做到这一点。基于各种互联网来源(其他问题/教程/等),我已经能够得到这个代码,但显然不工作,因为我现在甚至无法在模拟器中打开应用程序:Android:从数据库填充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的代码,因为这是工作的罚款之前,我添加了这段代码(我有它显示一个吐司与数据)。

谢谢!

回答

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是列表视图和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”;

+0

好消息是,至少现在控制台承认有错误。它在线上给我一个NullPointerException Cursor cursor = db.fetchAllNames();你知道为什么会发生这种情况吗?感谢所有的帮助! – Salmononius2

+0

检查你的“db”......它是罪魁祸首。 – kgandroid

+0

也不要忘记将你的主键定义为_id。 – kgandroid

0

这个问题我昨天刚刚回答。创建CursorLoader的最简单方法是,当活动创建并容易设置适配器时,将只从数据库加载数据到背景数据库中:只需从“(DB.COLUMN_NAME)”和“至”(R.id.textviewToDisplay)组成列。 请检查这个答案。 retriveing data from exisint sqlite database