how to set ListView background item color in android -
i have custom listview 3 textview. every row show person name, email , day.now want set red color person's days less 120 , want set green color person's days greater 120.how solve problem??any appreciated.
person_show
public class person_show extends activity { static int total_day; list<personinfo>personinfo; public myadapter adapter; static vector<string>email = new vector<>(); static vector<string>name = new vector<>(); static vector<string>city = new vector<>(); static string[] name,email,city; listview list; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.personlistview); firebase.setandroidcontext(this); //show_person(); person_show(); } private void person_show() { personinfo = new arraylist<>(); list = (listview)findviewbyid(r.id.list); string[] value = new string[]{" no data found"}; arrayadapter<string> adapter = new arrayadapter<string>(this,r.layout.persontextview,value); list.setadapter(adapter); personinfo.add(new personinfo("abc","abc@gmail.com","65 days")); personinfo.add(new personinfo("xyz","xyz@gmail.com","130 days")); personinfo.add(new personinfo("pqr","pqr@gmail.com","70 days")); personinfo.add(new personinfo("abc","abc@gmail.com","140 days")); for(int = 0;i<email.size();i++) personinfo.add(new personinfo(name.get(i),email.get(i),"150 days")); adapter = new myadapter(this,personinfo); list.setadapter(adapter); }
myadapter
public class myadapter extends baseadapter { list<personinfo>personinfo; context context; public myadapter(context context,list<personinfo>personinfo) { this.context = context; this.personinfo = personinfo; } @override public int getcount() { return personinfo.size(); } @override public object getitem(int position) { return personinfo.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); view view = inflater.inflate(r.layout.person_layout,parent,false); textview txt_name,txt_email,txt_phone; txt_name = (textview)view.findviewbyid(r.id.t_name); txt_email = (textview)view.findviewbyid(r.id.t_email); txt_phone = (textview)view.findviewbyid(r.id.t_phone); txt_name.settext(personinfo.get(position).getname()); txt_email.settext(personinfo.get(position).getemail()); txt_phone.settext(personinfo.get(position).getphone()); return view; } }
personinfo
public class personinfo { string name,phone,email; public personinfo(string name,string email,string phone) { this.name = name ; this.email = email; this.phone = phone; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } }
show adapter code . @ first check days
. days
holds no of days . days
should int
type .
check below logic
.
string[] splited = personinfo.get(position).getphone().split("\\s+"); //65 days string str_days=splited[0]; //65 int days = integer.valueof(str_days); if (days<=120) { your_layoutobj.setbackgroundcolor(color.parsecolor("#54d66a")); } else { your_layoutobj.setbackgroundcolor(color.parsecolor("#ffffff")); }
fyi
your_layoutobj
rootlayout .
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rootlayout" > ..... </relativelayout>
declare like textview , set background color
.
Comments
Post a Comment