2011-11-28 74 views
0

我正在从android网站的记事本教程工作。我创建了一个包含2个条目的数据库:名称和地址。当应用程序运行时,只显示名称。我想显示姓名和地址。我编辑了我的filldata方法,但它不起作用。看到我的代码如下。有什么建议么?SQLite更改列表的布局

private void fillData() { 
    Cursor notesCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(notesCursor); 

    // Create an array to specify the fields we want to display in the list (TITLE + BODY) 
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY}; 

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

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

回答

2

在你R.layout.notes_row文件,你应该有两个TextViews一个标题和一个地址,然后你会写:

String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY}; 

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

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

编辑: R.layout.notes_row可能是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
<TextView android:id="@+id/text1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
<TextView android:id="@+id/text2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
</LinearLayout> 
+0

谢谢。我试图在我的notes_row文件中创建第二个textview,并继续收到错误。我的代码如下所示。任何想法?'$ <?xml version =“1.0”encoding =“utf-8”?> DMC

+0

@ user876343请参阅我编辑的答案。在xml布局中,你必须只有一个根元素,因此你必须用另一个元素(我使用'LinearLayout',但还有其他选项)包装你的两个'TextView'。用我的上面的代码,你将有一个布局,两个'TextView',一个在另一个之上。 – Luksprog

+0

谢谢,我自己在那里工作!欢呼的帮助! – DMC