[英]Dynamically changing TextView color for Three different group in ListView
我正在嘗試開發像索引的ListView
。 在我的情況下,它們是列表,章,子章節和子項的三個級別。成功地實現了三個級別。
我想為所有三個列表更改三種不同的顏色。
嘗試過:
我嘗試了3級宜人的Listview,但我無法實現。
所以我用單個數組更動態地嘗試了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塊,並將所有值更改為青色。
返回convertView而不是super.getView(...)。 以下是一些示例: https : //github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
終於我找到了解決我問題的答案..這可能會幫助某人.. Expentable listview
實例,您可以使用listview
.. ThanQ eveyone ..
@Override
public View getView(int position,
View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View rowView = mInflater.inflate(R.layout.singlerow, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
RelativeLayout txt = ((RelativeLayout) rowView.findViewById(R.id.clicked));
textView.setText(Mainarray.get(position));
String s = Mainarray.get(position);
String s2 = s.substring (1,7);
String[] items = s2.split(":");
if (items.length == 1) {
//chapter color
txt.setBackgroundColor(Color.WHITE);
}else if(items.length == 2){
//subchapter color
txt.setBackgroundColor(Color.parseColor("#E6F5FF"));
}else if(items.length == 3){
//child color
txt.setBackgroundColor(Color.parseColor("#CCEBFF"));
}else{
// else block if u need
txt.setBackgroundColor(Color.MAGENTA);
}
return rowView;
}}
現在我的屏幕是:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.