2010-08-16 81 views
2

我想顯示一個列表視圖,當點擊時能夠獲得項目鍵值。我將如何去做。與關鍵字段列表視圖

感謝, 院長

+0

您需要更詳細地說明正是你的問題是什麼。展示一些代碼,展示你所做的,細節的細節! – binnyb 2010-12-28 22:56:57

回答

0

的項目列表視圖的鍵值?您是否在尋找onListItemClick from ListActivity

+0

不,我正在尋找每行存儲記錄ID,但顯示文本。密鑰是如何存儲的,並且與行項目相關但尚未顯示? 謝謝, 院長 – user412317 2010-08-18 20:43:37

+0

我想讀取一個數據庫並存儲一個record_id,比如說9104,並且列表中顯示的項目是「Blue Donkeys」,當我點擊「Blue Donkeys」時,我想獲得密鑰「9104 」。 任何想法? 謝謝, Dean – user412317 2010-08-18 21:10:39

+0

CursorAdapter? – fedj 2010-08-19 10:00:55

5

你想要做的事情的方式是使用帶有簡單類的ArrayAdapter來創建要使用的對象。

舉例來說,如果你想使人們的列表視圖,包括他們的姓名和年齡,並希望顯示他們只是在ListView名字,你會首先創建一個Person類,如下所示:

public class Person { 
    int age; 
    String name; 

    public Person(int age, String name) { 
    this.age = age; 
    this.name = name; 
    } 

    @Override 
    public String toString() { 
    return this.name; //what you want displayed for each row in the listview 
    } 

} 

然後,在多數民衆贊成利用列表視圖(說這就是所謂的PersonTracker.java)你的java文件,你會打電話:

setListAdapter(new ArrayAdapter<Person>(PersonTracker.this, R.layout.list_people, people); 
lv = getListView(); 
lv.setTextFilterEnabled(true); 

lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View currView, int position, long id) { 
    Person selected = (Person)lv.getItemAtPosition(position); 
    String selectedName = selected.name; //ideally this would probably be done with accessors 
    int selectedHeight = selected.height; 

    //Do whatever you need to with the name and height here 
    //such as passing via intents to the next activity... 

    } 
}); 

其中list_people只是一個通用的XML佈局只是一個TextView控制每個如何排看起來,人是包含lis的xml佈局tview。

正如你在上面看到的,在你的onItemClick函數中,你可以從與被點擊的列表項關聯的Person中獲得你想要的任何東西。

不管怎樣,希望可以幫助別人了,節省了我花想出來的時候......我想睡覺......