![](/img/trans.png)
[英]Items in listview not setting up using BaseAdapter and SimpleAdapter
[英]Setting tag for listview item when using SimpleAdapter
我想在每個ListView
項中存儲一些數據(比如說ID),然后在clickItem
監聽器上檢索它。
如果我創建自己的適配器,我知道如何做到這一點。 但是,如果我使用SimpleAdapter
是否可以為每個項目設置唯一標記?
如果不覆蓋至少getView()
,您將無法使用每個Adapter的視圖回收器設置標記。
但是,您可以簡單地使用TextView將自定義布局設置為GONE
或INVISIBLE
並綁定地圖列表中的數據( List<Map<String, ?>>
)。 稍后您可以在OnItemClickListener中輕松獲取此TextView。
我想如果你想通過像這樣的標簽唯一地識別imageview,這將有助於你
//add this in your getview() method
ImageView imageView = new ImageView(_context);
imageView.setTag(1);
然后在listview的/ imageView上點擊檢查它的標簽
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Tag = (Integer) arg1.getTag();
}}
SimpleAdapter中有一個方法。 它叫做ViewBinder。 嘗試在“SimpleAdapter adapter = new SimpleAdapter(this,Maps,R.layout.search,from,to);”和“setListAdapter(adapter);”(在onCreate方法內)之后立即包含此代碼。
@Override
public void onCreate(Bundle savedInstanceState) {
//...
String[] from = new String[] {"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN"};
int[] to = new int[] { R.id.textView_1, R.id.textView_2, R.id.textView_3, R.id.textView_4, R.id.textView_5, R.id.textView_6, R.id.textView_7};
SimpleAdapter adapter = new SimpleAdapter(this, Maps, R.layout.search, from, to);
SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object object, String value) {
System.out.println("view= "+view);
System.out.println("view.toString()= "+ view.toString());
System.out.println("view.getId()= "+ view.getId()); //The return value will be decimal (not hexadecimal). You can have this value as a global string for later use.
System.out.println("view.getVisibility()= "+ view.getVisibility());
System.out.println("view.equals((TextView) view.findViewById(R.id. textView_5))= "+ view.equals((TextView) view.findViewById(R.id.textView_5)));
if (view.equals((TextView) view.findViewById(R.id.textView_5)))
{
TextView textView_five = (TextView) view.findViewById(R.id. textView_5);
//Change color/answer/etc for textView_5
}
//OR
if (view instanceof TextView) {
//Do stuff
return true;
}
return false;
}
};
adapter.setViewBinder(binder);
setListAdapter(adapter);
}//End of onCreate
將為每個R.id.textView_1,R.id.textView_2,R.id.textView_3,R.id.textView_4,R.id.textView_5,R.id.textView_6,R.id.textView_7調用setViewValue方法有“適配器”。 每次View /每次正在繪制上述R.id之一時,都會調用setViewValue方法。
然后,當用戶單擊其中一個ListView項並且您想要更改它時,請覆蓋onListItemClick方法。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (v.equals((TextView) v.findViewById(R.id.textView_5)))
{
TextView textView_five = (TextView) v.findViewById(R.id. textView_5);
textView_five.setText(“stuff”);
//Change color/answer/etc for textView_5
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.