2011-05-12 71 views
1

我該如何擴展我的例子以使用雙行列表視圖(例如從數據庫中讀出電子郵件)?如何在android listview中使用simple_list_item_2?

ArrayList<String> db_results = new ArrayList<String>(); 
    Cursor c = db.rawQuery("SELECT lastname, firstname FROM contacts ORDER BY lastname",null); 
    while(c.moveToNext()) { 
     db_results.add(String.valueOf(c.getString(c.getColumnIndex("lastname"))) + ", " + String.valueOf(c.getString(c.getColumnIndex("firstname")))); 
    } 
    c.close(); 
    lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,db_results)); 

謝謝...

回答

4

你爲什麼不使用SimpleCursorAdapter?這是一個例子。

// SimpleListAdapter is designed for binding to a Cursor. 
     ListAdapter adapter = new SimpleCursorAdapter(
       this, // Context. 
       android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor 
rows). 
       mCursor,            // Pass in the cursor to bind to. 
       new String[] {People.NAME, People.COMPANY},   // Array of cursor columns to bind to. 
       new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns. 

     // Bind to our new adapter. 
     setListAdapter(adapter); 
+0

嗨,謝謝,我試過了,但是我遇到了一個問題,即字段_id是必需的。這對我來說不是解決方案,因爲我只有字段ID(它在我的桌面應用程序中使用,並且無法更改)。有沒有辦法讓我的代碼上面有2列? – fillibuster 2011-05-12 21:29:53

+1

對不起。這是一個簡單的解決方案:SELECT id as _id – fillibuster 2011-05-12 21:48:35

相關問題