简体   繁体   English

如何更改列表视图的背景颜色?

[英]how to change the background color of a listview?

my array list data:我的数组列表数据:
John约翰
John约翰
Jack杰克
Peter彼得
Parker派克

the repeated names must have different background in the list view列表视图中重复的名称必须具有不同的背景

how to do that?怎么做?

ArrayList<listitem> data=
(ArrayList<listitem>)bundle.getSerializable("value");

sentItems = data; //names John,John,Jack....

final List<listitem> duplicates = new ArrayList<listitem>();
Set<listitem> listitemSet = new TreeSet<listitem>(new Comparator<listitem>() {
@Override
public int compare(ListItem o1, ListItem o2) {
return o1.getTitle().compareTo(o2.getTitle());
}
});
for(ListItem c : sentItems)
{
if(!listitemSet.add(c))
{
duplicates.add(c); //similar names added to the new list
}
}

Make an array as given below as no of list item i suppose u have five items制作一个如下所示的数组作为列表项的编号,我想你有五个项目

int[] color_arr={Color.BLUE,Color.CYAN,Color.DKGRAY,Color.GREEN,Color.RED};

and after do in ur getView method of custome adapter as below然后在你的客户适配器的 getView 方法中做如下

public View getView(int position, View convertView, ViewGroup parent)
 {

 LayoutInflater inflater = getLayoutInflater();
 View row=convertView;

 row = inflater.inflate(R.layout.listview_custome, parent, false);
 row.setBackgroundColor(color_arr[position]);// this set background color

 TextView textview = (TextView) row.findViewById(R.id.tv_list);
 ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);

 textview.setText(data_text[position]);
 imageview.setImageResource(data_image[position]);

 return (row);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM