2012-02-17 28 views
0

我有一個從項目與返回列表視圖中的文本onListItemClick:onListItemClick從該項目當前視圖頂部返回數據

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    // Get the item that was clicked 
    TextView tv = (TextView)findViewById(R.id.item_title); 
    Toast.makeText(getApplicationContext(), (tv).getText(), 
      Toast.LENGTH_SHORT).show(); 
} 

的問題是它返回的數據是從該項目的當前列表視圖的頂部沒有被點擊的實際列表項目。爲了說明,如果我有像這樣

A 
----- (Start of Viewable area) 
B 
C 
D 
------(End of viewable area) 
E 

列表如果我點擊項目d土司將返回標題爲項目B.任何想法我怎麼能解決這個問題?

+0

你不能使用類似'l.getItemAtPosition(position))'的東西嗎? – 2012-02-17 21:19:59

+0

你的名單有什麼類型的數據?字符串或textview? – kosa 2012-02-17 21:21:42

+0

Textview和Imageview。 – Nick 2012-02-17 21:22:32

回答

2

該方法從您的自定義ListView中調用,是否正確?從我所知道的情況來看,「findViewById」是在整個ListView中搜索ID爲「R.id.item_title」的視圖。由於屏幕上有多個列表項目,因此它將拉動頂部項目。相反,請致電

// Call on the child view provided as a parameter, instead of on the ListView 
TextView tv = (TextView)v.findViewById(R.id.item_title); 

以便它搜索作爲參數「v」傳遞的View對象的子視圖。

+0

天才!您在所有帳戶中都是正確的,謝謝:) – Nick 2012-02-17 21:54:16