[英]Setting tag for listview item when using SimpleAdapter
I would like to store some data (let's say ID) in each ListView
item and retrieve it later on clickItem
listener. 我想在每个
ListView
项中存储一些数据(比如说ID),然后在clickItem
监听器上检索它。
I know how to do that if I create my own adapter. 如果我创建自己的适配器,我知道如何做到这一点。 But is it possible to set a unique tag for each item if I am using
SimpleAdapter
? 但是,如果我使用
SimpleAdapter
是否可以为每个项目设置唯一标记?
Without overriding at least getView()
, you will have trouble setting tags with every Adapter's view recycler. 如果不覆盖至少
getView()
,您将无法使用每个Adapter的视图回收器设置标记。
However you can simply pass a custom layout with a TextView that has its visibility set to GONE
or INVISIBLE
and bind data from your List of Maps ( List<Map<String, ?>>
). 但是,您可以简单地使用TextView将自定义布局设置为
GONE
或INVISIBLE
并绑定地图列表中的数据( List<Map<String, ?>>
)。 Later you can easily fetch this TextView in an OnItemClickListener. 稍后您可以在OnItemClickListener中轻松获取此TextView。
i think this will help you in case you want to identify imageview uniquely by tag like this 我想如果你想通过像这样的标签唯一地识别imageview,这将有助于你
//add this in your getview() method
ImageView imageView = new ImageView(_context);
imageView.setTag(1);
and then on listview's/imageView's click check its tag like this 然后在listview的/ imageView上点击检查它的标签
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Tag = (Integer) arg1.getTag();
} } }}
There is a method inside SimpleAdapter for that. SimpleAdapter中有一个方法。 It's called ViewBinder.
它叫做ViewBinder。 Try to include this code immediately after “SimpleAdapter adapter = new SimpleAdapter(this, Maps, R.layout.search, from, to);” and before “setListAdapter(adapter);” (Inside your onCreate Method).
尝试在“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
setViewValue method will be called for each 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 that you have in “adapter”. 将为每个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方法有“适配器”。 The setViewValue method will be called each View/each time one of the above R.id's is being drawn.
每次View /每次正在绘制上述R.id之一时,都会调用setViewValue方法。
Then, when the user clicks on one of the ListView items and u want to change it, override onListItemClick Method. 然后,当用户单击其中一个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.