2012-01-12 24 views
0

我收到了ContentProvider,它提供了一些內容,例如過濾器。 這些將在ListView中呈現。因爲過濾器有很多字段需要爲列表項創建一個自己的視圖。這些字段映射到擴展CursorAdapter的類中。在CursorAdapter中保存URI/id

public void bindView(View view, Context context, Cursor cursor) { 

    TextView searchPattern = (TextView) view.findViewById(R.id.tv_searchpattern); 
    TextView searchType = (TextView) view.findViewById(R.id.tv_searchtype); 

    int type = cursor.getInt(FilterProvider.SEARCH_TYPE_COLUMN); 
    [...] 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final View view = inflater.inflate(R.layout.group_list_item, parent, false); 
    return view; 
} 

但現在我想知道如何將「內容uris」與列表項一起「攜帶」。 這樣我以後可以輕鬆地檢索它們以便對項目進行操作(例如更新,刪除)?

使用View.id字段是一個好主意嗎?

view.setId(cursor.getInt(FilterProvider.KEY_COLUMN)); 

還是我完全在錯誤的軌道上?我需要擔心,因爲Integer實際上是Sqlite中的一個Long嗎?

回答

0

errr,我不會弄亂那樣的視圖ID。這似乎不是一個好主意。我不能說我有任何確鑿的證據表明這一點,但遊標適配器正在處理視圖,誰來說未來的平臺版本對子視圖ID沒有任何重要意義?

然而,有你想要的完美的方法:

view.setTag(Object someObject); 

http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object

所以,你的情況,你可以這樣做:

view.setTag(new Integer(cursor.getInt(FilterProvider.KEY_COLUMN))); 

,然後進行檢索無論你在哪裏,你都會這樣做:

Integer key = (Integer) view.getTag(); 

我希望有幫助!