android - ArrayAdapter row color method changes multiple rows -


i'm attempting make arrayadapter custom list view. when user clicks on "finish" on listview, want row color particular entry turn red hide "finish" button , show different button called "reset". far works great changes row , button configuration of rows further down in scrollable list.

i've looked @ number of different solutions , i've tried i've seen suggested. must missing something...

public class resultsadapter extends baseadapter { private static string logtag = "logtag: " + thread.currentthread()         .getstacktrace()[2].getclass().getsimplename(); // log tag records context mcontext; // add context layoutinflater inflater; // instance of inflater resultdatasource resultdatasource; // lists of result private list<result> maindatalist = null; private arraylist<result> arraylist;  // instance constructor public resultsadapter(context context, list<result> maindatalist,                       resultdatasource resultdatasource) {     this.resultdatasource = resultdatasource;     mcontext = context;     inflater = layoutinflater.from(mcontext);     // create copy of main list don't end damagaing original list     this.arraylist = new arraylist<result>();     this.arraylist.addall(maindatalist);  }  @override public int getcount() {     return arraylist.size(); }  @override public object getitem(int position) {     return arraylist.get(position); }  @override public long getitemid(int position) {     return position; }  @override public view getview( int index, view view, final viewgroup parent) {      if (view == null) {         // build inflater create new row each row in results table         layoutinflater inflater = layoutinflater.from(parent.getcontext());         view = inflater.inflate(r.layout.activity_list_template_results, parent, false);     }        log.i("results adapter ", "maindatalist of size " + arraylist.size() + " index " + index);     // create result hold data row item.     final result result = arraylist.get(index);      // instance of finish button     final button btnfinish = (button) view.findviewbyid(r.id.btn_boat_finished);      // instance of finish button     final button btnreset = (button) view.findviewbyid(r.id.btn_boat_reset_finished);      // wire text views , set associated text them.     final textview tv1 = (textview) view.findviewbyid(r.id.txt_hd_results_id);     tv1.settext(result.getresultsid() + "");      final textview tv2 = (textview) view.findviewbyid(r.id.txt_hd_results_race_id);     tv2.settext(result.getresultsraceid() + "");     final textview tv3 = (textview) view.findviewbyid(r.id.txt_hd_results_boat_id);     tv3.settext(result.getresultsboatid() + "");     final textview tv4 = (textview) view.findviewbyid(r.id.txt_hd_results_visible);     tv4.settext(result.getresultsvisible() + "");     final textview tv5 = (textview) view.findviewbyid(r.id.txt_hd_results_name);     tv5.settext(result.getboatname() + "");     final textview tv6 = (textview) view.findviewbyid(r.id.txt_hd_results_class);     tv6.settext(result.getboatclass() + "");     final textview tv7 = (textview) view.findviewbyid(r.id.txt_hd_results_sailnum);     tv7.settext(result.getboatsailnum() + "");     final textview tv8 = (textview) view.findviewbyid(r.id.txt_hd_finish_time);     if (result.getresultsboatfinishtime() != null) {         tv8.settext(result.getresultsboatfinishtime() + "");     } else {         tv8.settext("");     }      final view finalview = view;      // add text views list     final arraylist<textview> textviews = new arraylist<textview>() {{         add(tv1);         add(tv2);         add(tv3);         add(tv4);         add(tv5);         add(tv6);         add(tv7);         add(tv8);     }};      // programmatically change text style.     (textview t : textviews) {         t.settextcolor(color.parsecolor("#000000")); // make text black         t.settypeface(typeface.default_bold); // make text bold     }      result r = arraylist.get(index);      // on refresh of list.     // if there finish time present make row red     if (r.getresultsboatfinishtime() != null) {          //hide finish button , show reset button instead.         btnfinish.setvisibility(view.gone);         btnreset.setvisibility(view.visible);         // set text of reset button. click count reset 3         btnreset.settext("reset [3]");         //set row color red         view.setbackgroundcolor(view.getresources().getcolor(r.color.red06));         //set text of each text box white         (textview t : textviews) {             t.settextcolor(color.parsecolor("#ffffff")); // make text white         }     }           // set function of each finish button         btnfinish.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 localtime localtime = localtime.now();// capture current time                 //format time string                 string timeformatted = globalcontent.localtimetostring(localtime);                 //insert time database.                 resultdatasource.updatefinishtime(result.getresultsid(), timeformatted);                 //set result entry's finish time same.                 result.setresultsboatfinishtime(timeformatted);                 log.i(logtag, "finish time " + result.getboatname() + " is: " + localtime + " formatted: " + timeformatted);                 //hide finish button , show reset button instead.                 btnfinish.setvisibility(view.gone);                 btnreset.setvisibility(view.visible);                 // set text of reset button. click count reset 3                 btnreset.settext("reset [3]");                 //set row color red                 finalview.setbackgroundcolor(v.getresources().getcolor(r.color.red06));                 //set text of each text box white                 (textview t : textviews) {                     t.settextcolor(color.parsecolor("#ffffff")); // make text white                 }                 notifydatasetchanged();             }         });      // resets finish time 0 after 3 clicks     btnreset.setonclicklistener(new view.onclicklistener() {         int counter = 3; // initial click count         @override         public void onclick(view v) {             string message; // message pass user             if (counter == 1) { // check how many clicks left, if 1 then...                 //blank out database entry                 resultdatasource.updatefinishtime(result.getresultsid(), null);                 //blank out result object fnish time entry                 result.setresultsboatfinishtime(null);                 //hide reset button , show finish button                 btnreset.setvisibility(view.gone);                 btnfinish.setvisibility(view.visible);                 counter = 3; // reset counter                 //output message                 message = result.getboatname() + "'s finish time reset";                 //set row color transparent                 finalview.setbackgroundcolor(color.parsecolor("#00000000"));                 // make each text box text black                 (textview t : textviews) {                     t.settextcolor(color.parsecolor("#000000")); // make text black                  }                 notifydatasetchanged();             } else {                 counter--; // decrement click counter                 //state how many clicks left in toast menu                 message = "to reset, click \"reset\" " + counter + " more times";                 //change reset button text indicate how many clicks left.                 btnreset.settext("reset [" + counter + "]");             }             toast.maketext(v.getcontext(), message, toast.length_long).show();           }     });      return view; }  //filter results based on text entered text box in results menu public void filter(string chartext) {     chartext = chartext.tolowercase(locale.getdefault());     maindatalist.clear();     if (chartext.length() == 0) {         maindatalist.addall(arraylist);     } else {         (result result : arraylist) {             if (result.getboatname().tolowercase(locale.getdefault())                     .contains(chartext)) {                 maindatalist.add(result);             }         }     }     notifydatasetchanged(); } } 

if (r.getresultsboatfinishtime() != null) {      /* ... */      //set row color red     view.setbackgroundcolor(view.getresources().getcolor(r.color.red06));      /* ... */ } 

you're forgetting view recycling. view you've colored red being re-used show other rows well. since you've set background color red view, other rows have red background well.

a simple solution add else branch:

} else {     view.setbackgroundcolor(view.getresources().getcolor(r.color.myoriginalcolor)); } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -