android listview set background of some items (problems when scrolling) -


i´m new android , i´ve problem items-background in listview... seems work (items "premium = 1" should have yellow background), when scroll down , everytime other items have yellow background-color?!

and can´t find mistake :(

pois.java (the important part)

protected void ausgeben() {        ausgabestring = ausgabestring.trim();     ausgabestring = ausgabestring.substring(1);     rowitems = new arraylist<poirowitem>();      try{                     jsonarray = new jsonarray(ausgabestring);         log.i("ausgabe", "teilsuccess");         for(int i=0; < jsonarray.length(); i++)         {             jsonobject jsonobj = jsonarray.getjsonobject(i);             poirowitem item = new poirowitem(jsonobj.getstring("poi_name"), jsonobj.getstring("poi_id"), jsonobj.getstring("poi_premium"));             rowitems.add(item);         }     }     catch(jsonexception e)     {         log.e("log_tag", "error parsing data "+e.tostring());     }      listview = (listview) findviewbyid(r.id.list);     poilistviewadapter adapter = new poilistviewadapter(this, r.layout.poi_list_item, rowitems);     listview.setadapter(adapter);     //listview.setonitemclicklistener(this);    } 

poilistviewadapter.java (where think problem)

import java.util.list; import at.visualstudioteschl.dguide.poirowitem; import at.visualstudioteschl.dguide.r; import android.app.activity; import android.content.context; import android.graphics.color; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview;  public class poilistviewadapter extends arrayadapter<poirowitem> {  context context;  public poilistviewadapter(context context, int resourceid, list<poirowitem> items) {     super(context, resourceid, items);     this.context = context; }  private class viewholder {     textview txttitle; }  public view getview(int position, view convertview, viewgroup parent) {     viewholder holder = null;     poirowitem rowitem = getitem(position);      layoutinflater minflater = (layoutinflater) context             .getsystemservice(activity.layout_inflater_service);      if (convertview == null) {         convertview = minflater.inflate(r.layout.poi_list_item, null);         holder = new viewholder();         holder.txttitle = (textview) convertview.findviewbyid(r.id.title);         convertview.settag(holder);     } else         holder = (viewholder) convertview.gettag();         holder.txttitle.settext(rowitem.gettitle());          // todo hier gibts noch nen fehler... es werden beim scrollen nach kurzer zeit alle gelb         if (rowitem.getpremium().contains("1")){             holder.txttitle.setbackgroundcolor(color.yellow);         }       return convertview; } } 

poirowitem.java

public class poirowitem { private string title; private string kat_id; private string premium;  public poirowitem(string title, string kat_id, string premium) {     this.title = title;     this.kat_id = kat_id;     this.premium = premium; }  public string getid() {     return kat_id; } public void setid(string id) {     this.kat_id = id; }  public string gettitle() {     return title; } public void settitle(string title) {     this.title = title; } @override public string tostring() {     return kat_id; }      public string getpremium() {     return premium; } public void setpremium(string premium) {     this.premium = premium; } } 

*poi_list_item.xml*

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" >  <textview     android:id="@+id/title"     android:layout_width="fill_parent"     android:layout_height="wrap_content"          android:gravity="left"     android:padding="5dp"     android:textsize="20sp"     android:singleline="true"/> </relativelayout> 

the views re-used.. need set original color when don't want yellow color.

// todo hier gibts noch nen fehler... es werden beim scrollen nach kurzer zeit alle gelb if (rowitem.getpremium().contains("1")){     holder.txttitle.setbackgroundcolor(color.yellow); } else{     holder.txttitle.setbackgroundcolor(color.white); // original color here } 

Comments