[英]how to set ListView background item color in android
I have a Custom Listview with three TextView. 我有一个带有三个TextView的Custom Listview。 Every row will show person name, email and day.Now I want to set a red color for which person's days are less than 120 and I also want to set a green color for which person's days are greater than 120.How to solve my problem??Any help will be appreciated. 每行都会显示人名,电子邮件和日期,现在我要设置一个人日少于120的红色,我还想设置一个人日大于120的绿色。如何解决我的问题??任何帮助将不胜感激。
My ListView Image 我的ListView图像
person_show 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 i = 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 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;
}
}
Add This code in Adapter or View Holder if you use RecyclerView 如果您使用RecyclerView,则在适配器或视图架中添加此代码
if(yourObject.days>120){
rootLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.green));
}else{
rootLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
}
Show your Adapter code . 显示您的适配器代码。 At first Check days
. 在最初的检查days
。 days
holds no of days . days
无天数。 days
should be int
type . days
应该是int
类型。
Check below LOGIC
. 检查以下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
is your RootLayout . 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 and Set background color
. 声明LIKE Textview and Set background color
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.