简体   繁体   中英

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.

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 ?

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();

} }

There is a method inside SimpleAdapter for that. It's called 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).

@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”. The setViewValue method will be called each View/each time one of the above R.id's is being drawn.

Then, when the user clicks on one of the ListView items and u want to change it, override onListItemClick Method.

@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
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM