【发布时间】:2015-02-14 12:41:07
【问题描述】:
我正在尝试开发像索引一样的ListView。在我的情况下,它们是三个级别的列表、章节、子章节和子......我成功地实现了三个级别。
我想为所有三个列表更改三种不同的颜色。
试过了:
我尝试了 3 级可扩展列表视图,但我无法实现。
所以我尝试使用单个数组更动态地使用 Listview..
现在我想通过getview方法改变listview中的列表颜色
这是我的 getview() 用于将子章节添加到列表:
for (int i = listsubchapt.size() - 1; i >= 0; i--) {
Mainarray.add(pos + 1, listsubchapt.get(i));
Mainarrayid.add(pos + 1, listsubchaptid.get(i));
//System.out.println("mainarraywithsub==++ " + Mainarray);
ArrayAdapter<String> adapter = (new ArrayAdapter<String>(
IndexChapter.this, R.layout.singlerow,
R.id.textView1, Mainarray){@Override
public View getView(int position,
View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.singlerow, null);
for(int i=0;i<Mainarray.size();i++){
String sample=Mainarray.get(i);
String[] items = sample.split(":");
if (items.length == 1) {
TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
txt.setBackgroundColor(Color.CYAN);
}else if(items.length == 2){
TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
txt.setBackgroundColor(Color.BLUE);
}else if(items.length == 3){
TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
txt.setBackgroundColor(Color.GRAY);
}else{
TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
txt.setBackgroundColor(Color.CYAN);
}
}
return super.getView(position, convertView, parent);
}});
clientdetailslist.setAdapter(adapter);
clientdetailslist.setSelection(pos-1);
adapter.notifyDataSetChanged();
}
编辑:
它移动到 length=1 块并将所有值更改为青色。
【问题讨论】:
-
你有什么问题?
-
@SweetWishershi 我想给章节、子章节和孩子三种不同的颜色
-
那么
txt.setBackgroundColor(Color.BLUE);这是做什么的? -
return convertView而不是return super.getView(position, convertView, parent); -
@SweetWishershi 列表中的所有值都变为蓝色.. 但我想单独更改子章节变为蓝色
标签: android listview android-listview getview