1

我正在尝试填充ListView...。但我无法从...中使用的数据库中添加 2 个字符串值。SimpleCursorAdapter任何一个,帮帮我

代码

列表视图.java

public class ListView extends ListActivity {

SQLiteDatabase messagedb;
List<String> senderArray = new ArrayList<String>();
List<String> bodyArray = new ArrayList<String>();

String[] simpleSenderArray = new String[ senderArray.size() ];

String[] simpleBodyArray = new String[ bodyArray.size() ];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_list);

    senderArray.toArray( simpleSenderArray );
    bodyArray.toArray( simpleBodyArray );

    messagedb=ListView.this.openOrCreateDatabase("message",0, null);

            //This database created already and add sender and body values...here just open that database
    Cursor  cur=messagedb.rawQuery("select sender, body from tab2", null);
    while(cur.moveToNext())
    {
        String sender = cur.getString(cur.getColumnIndex("sender"));
        String body = cur.getString(cur.getColumnIndex("body"));
        senderArray.add(sender);
        bodyArray.add(body);
    }
    cur.close();


    messagedb.close();

     int[] to = new int[] { R.id.sender_entry, R.id.body_entry };

     SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur,simpleSenderArray, to);

     this.setListAdapter(mAdapter);


}

}

my_list.xml

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

<ListView

    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />


</LinearLayout>

my_list_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<TextView
    android:id="@+id/sender_entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dip" />
 <TextView
    android:id="@+id/body_entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dip" />
</LinearLayout>

有人可以帮我填充我的ListView吗?

4

1 回答 1

1

您已经将 Activity 命名ListView为内置类名,我强烈建议您将其更改为独特的名称,以防止任何混淆或命名冲突。

在你的SQLite表中,你应该有一个_id INTEGER列设置为你自己。)PRIMARY KEY_id

    messagedb.execSQL(
        "CREATE TABLE IF NOT EXISTS tab2(" +
        " _id INTEGER PRIMARY KEY," +
        " sender INT(13)," +
        " body varchar)");

您的代码应该看起来更像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_list);

    // Using a SQLiteOpenHelper might be best, but I'll assume that this command works
    messagedb=ListView.this.openOrCreateDatabase("message",0, null);

    Cursor cur = messagedb.rawQuery("select _id, sender, body from tab2", null);
    String[] from = new String[] { "_id", "sender", "body" }
    int[] to = new int[] { R.id.sender_entry, R.id.body_entry };

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur, from, to);

    this.setListAdapter(mAdapter);
}

您可能要考虑使用 SQLiteDatabase.query() 方法,我相信它会更快一点:

Cursor cur = messagedb.query("tab2", new String[] {"_id", "sender", "body"}, null, null, null, null, null);

我还建议为所有列名定义静态变量,以方便将来编码。

最后,这应该适用于没有 _id 列的表,但我再次不推荐它:

Cursor cur = messagedb.rawQuery("select rowid as _id, sender, body from tab2", null);
于 2012-07-02T16:49:05.887 回答