2011-02-23 34 views
0

我正在使用自定義的ListView,並且爲每一行數據添加特定的ID以解決它並進一步處理它。有沒有什麼辦法,如何在XML文件中設置Id,如@ + id/idImageView + i,其中我是i ++?有什麼辦法嗎?Android - 動態使用

謝謝

回答

2

創建您自己的適配器並將其添加到ListView。適配器可以基於Adapter或它的子類。 根據需要重寫getItemId()方法。

ArrayAdapter的簡單示例,其中包含具有id屬性的對象。

public class MySimpleAdapter extends ArrayAdapter<MySimpleObject> { 

    public MySimpleAdapter(Context context, int resource, int textViewResourceId, List<MySimpleObject> objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //here create or recycle your view 
    } 

    @Override 
    public long getItemId(int position) { 
     return getItem(position).id; 
    } 


    public static class MySimpleObject { 
     long id; 
     String text; 
    } 
} 

對於數組中的簡單對象here is example

要使用字符串中的XML看here

+0

謝謝,我以爲我已經設置它首先要事後能夠獲得這個項目的ID,對嗎? – Waypoint 2011-02-23 10:14:52

+0

編輯我的答案。希望有幫助 – pawelzieba 2011-02-23 10:34:26

+0

如果有幫助,請投票。 – pawelzieba 2011-02-23 11:50:49

0

究竟是你想做些什麼?爲什麼不能使用位置信息並將其映射到您的數據數組。在ListView中顯示的行將被回收,這意味着如果您有1000行數據,則不會顯示唯一的1000行。我猜想只有10個,或者可能更少,視圖是獨一無二的,每次向上或向下滾動時都會回收它們。所以你不能使用這些行的id來唯一地獲取/識別你的數據。請參閱下面的文章。

http://android.amberfog.com/?p=296