android - ListView selector problem: Selection does not get removed -


i have listview. when click on listitem, set background of listitem (it's view) color:

  listview.setonitemclicklistener(new onitemclicklistener() {                 @override                 public void onitemclick(adapterview<?> a, view v, int position, long id) {                     setupdetailview(position);                     setupchartview(position);                     setuparview(position);                     emptyview.setvisibility(view.invisible);                      quotesadapter.isselected = true;                     //v.setbackgroundresource(r.drawable.stocks_selector);                 }             }); 

here adapter:

private class quoteadapter extends arrayadapter<quote> {          private arraylist<quote> items;         public boolean isselected = false;          public quoteadapter(context context, int textviewresourceid, arraylist<quote> items) {             super(context, textviewresourceid, items);             this.items = items;         }          @override         public view getview(int position, view convertview, viewgroup parent) {             view v = convertview;             if (v == null) {                 layoutinflater vi = (layoutinflater) getsystemservice(context.layout_inflater_service);                 v = vi.inflate(r.layout.mainrow, null);                  if(isselected)                     v.setbackgroundresource(r.drawable.red);                 else                     v.setbackgroundresource(r.drawable.transparent_background);             } 

the problem is, if select multiple rows, multiple rows have colored background. want clicked item have colored background. if click on row 2, want turn red, if click row 1, want row 2 go normal, , row 1 turn red.

how can this?

put list selector in listview

 <listview         android:id="@+id/list"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:cachecolorhint="#00000000"         android:listselector="@drawable/stocks_selector" /> 

your stocks_selector.xml should

<shape     xmlns:android="http://schemas.android.com/apk/res/android"     type="rectangle"  >     <gradient         android:startcolor="@color/start_gradient"         android:endcolor="@color/end_gradient"          android:angle="270" /> /> </shape> 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -