在android中更改listview子行的颜色
我一直在尝试更改listview中行的颜色,这取决于数据库的解析字符串满足的条件。我不知道怎样才能做到这一点 我知道似乎有关于如何做到这一点的例子,但我真的感到不知所措 我试过这个:在android中更改listview子行的颜色,android,listview,Android,Listview,我一直在尝试更改listview中行的颜色,这取决于数据库的解析字符串满足的条件。我不知道怎样才能做到这一点 我知道似乎有关于如何做到这一点的例子,但我真的感到不知所措 我试过这个: Calms info = new Calms(this); info.open(); String data = info.getFlareData(); info.close(); String arr[] = data.split("..\n\n"); System.out.println(arr); L
Calms info = new Calms(this);
info.open();
String data = info.getFlareData();
info.close();
String arr[] = data.split("..\n\n");
System.out.println(arr);
ListView listView = getListView();
System.out.println("$$$$$$$$$$$$$$$"+listView.getChildCount()+"$$$$$$$$$$$$$$");
setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, arr));
//System.out.println("****************"+getListView().getChildCount()+"**************");
for(int i = 0; i < arr.length; i++){
System.out.println("arr["+i+"] = " + arr[i].trim());
if(arr[i].contains("High Severity"))
{
// String highArr = arr[i];
listView.getChildAt(i).setBackgroundColor(Color.RED);
}
else if(arr[i].contains("Low Severity"))
{
listView.getChildAt(i).setBackgroundColor(Color.GREEN);
}
else if(arr[i].contains("Medium Severity"))
{
listView.getChildAt(i).setBackgroundColor(Color.rgb(255, 136, 0));
}
}
Calms info=新Calms(本);
info.open();
字符串数据=info.getFlareData();
info.close();
字符串arr[]=data.split(“..\n\n”);
系统输出打印项次(arr);
ListView ListView=getListView();
System.out.println(“$$$$$$$$$”+listView.getChildCount()+“$$$$$$”;
setListAdapter(新的ArrayAdapter(此,R.layout.simple_列表_项_1,arr));
//System.out.println(“*************”+getListView().getChildCount()+“*************”);
对于(int i=0;i
您应该为您的列表视图
编写自己的适配器
。在适配器中,您可以毫无问题地为每个项目设置背景色
有几个教程可用于在web上编写自定义适配器:
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, arr){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView) super.getView(position, convertView, parent);
String str = tv.getText().toString();
if(str.contains("High Severity"))
{
// String highArr = arr[i];
tv.setBackgroundColor(Color.RED);
}
else if(str.contains("Low Severity"))
{
tv.setBackgroundColor(Color.GREEN);
}
else if(str.contains("Medium Severity"))
{
tv.setBackgroundColor(Color.rgb(255, 136, 0));
}
return tv;
}
};
setListAdapter(ad);
ArrayAdapter ad=新的ArrayAdapter(这个,R.layout.simple\u list\u item\u 1,arr){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
TextView tv=(TextView)super.getView(位置、转换视图、父级);
String str=tv.getText().toString();
如果(str.contains(“高严重性”))
{
//字符串高arr=arr[i];
tv.setBackgroundColor(Color.RED);
}
否则如果(str.contains(“低严重性”))
{
tv.setBackgroundColor(Color.GREEN);
}
否则,如果(str.contains(“中等严重性”))
{
tv.setBackgroundColor(Color.rgb(255,136,0));
}
返回电视;
}
};
setListAdapter(ad);
您需要扩展阵列适配器。在方法上:
public View getView(int position, View convertView, ViewGroup parent)
您可以更改行的颜色。例如:
public class OneRecordAdapter extends ArrayAdapter<String> {
private ArrayList<String> arhmAllForList;
private Context context;
public OneRecordCursorAdapter(Context context, ArrayList<String> hm) {
super(context, R.layout.raw_checking_knowledge, hm);
arhmAllForList = hm;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(
R.layout.row, parent, false);
...
if ( /*some condition*/ ) {
rowView.setBackgroundColor(Color.RED);
} else {
rowView.setBackgroundColor(Color.GREEN);
}
...
return rowView;
}
}
公共类OneRecordAdapter扩展了ArrayAdapter{
私有ArrayListString arhmAllForList;
私人语境;
public OneRecordCursorAdapter(上下文上下文,ArrayListString hm){
super(上下文,R.layout.raw\u checking\u knowledge,hm);
arhmAllForList=hm;
this.context=上下文;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(
R.layout.row,父级,false);
...
如果(/*某些条件*/){
rowView.setBackgroundColor(颜色:红色);
}否则{
rowView.setBackgroundColor(Color.GREEN);
}
...
返回行视图;
}
}