我想在每个ListView
项目中存储一些数据(比如说 ID)并稍后在clickItem
侦听器中检索它。
如果我创建自己的适配器,我知道该怎么做。但是,如果我正在使用,是否可以为每个项目设置一个唯一的标签SimpleAdapter
?
我想在每个ListView
项目中存储一些数据(比如说 ID)并稍后在clickItem
侦听器中检索它。
如果我创建自己的适配器,我知道该怎么做。但是,如果我正在使用,是否可以为每个项目设置一个唯一的标签SimpleAdapter
?
Without overriding at least getView()
, you will have trouble setting tags with every Adapter's view recycler.
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, ?>>
). Later you can easily fetch this TextView in an OnItemClickListener.
i think this will help you in case you want to identify imageview uniquely by tag like this
//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.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
setViewValue 方法将为您调用的每个 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 方法将在每个视图/每次绘制上述 R.id 之一时被调用。
然后,当用户单击 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
}
}