java - List Selection Item -


i made scroll list multiple items. when click on 1 item color of background of item changes.

public class mylist extends activity {  packagemanager packman; public static arraylist<itemlist> list; private arrayadapter<itemlist> adapter;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.items);     init();     create();     addclicklistener(); }  public void init(){     if(list==null) {         list = new arraylist<itemlist>();         packman = getpackagemanager();         intent = new intent(intent.action_main, null);         i.addcategory("com.example.example");         list<resolveinfo> items = packman.queryintentactivities(i, 0);         (resolveinfo ri : items) {             itemlist item = new itemlist();             item.addname((string) ri.loadlabel(packman));             item.addnamepackage(ri.activityinfo.name);             item.addicon(ri.activityinfo.loadicon(packman));             list.add(item);         }     } }   private void create(){     v = (listview)findviewbyid(r.id.list);     adapter = new arrayadapter<itemlist>(this, r.layout.items, list) {         @override         public view getview(int position, view convertview, viewgroup parent) {             if(convertview == null) {                 convertview = getlayoutinflater().inflate(r.layout.items, null);                 imageview icon = (imageview) convertview.findviewbyid(r.id.icon);                 icon.setimagedrawable(list.get(position).geticon());                 textview name = (textview) convertview.findviewbyid(r.id.name);                 name.settext(list.get(position).getname());             }             return convertview;         }     };     v.setadapter(adapter); }  private void addclicklistener(){     v.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> av, view v, int pos, long id) {                av.getchildat(pos).setbackgroundcolor(color.parsecolor("#ddffff"));         }     }); } } 

i having problem. when select 1 item others selected automatically. how can resolve problem? thanks.

from code seems on click on item set background color ddffff, never switch them original color once "unselected" . can either:

  • keep track of selected item, , switch white before coloring new 1 (good if have 1 selected item @ time)

  • whiten on click, , color new one

  • keep track of state of each item list of booleans (active/inactive), , override draw loop color according state


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -