2016-12-27 28 views
1

的我的下一个estructure:更改文本颜色编程ListView中谁依赖arrayadapter

ArrayList<String> arrayList = new ArrayList<>(); 
list = (ListView) findViewById(R.id.list1); 

arrayAdapter = new ArrayAdapter<String>(INGR_NOTAS.this, R.layout.list_item_clickable, R.id.txtitem, arrayList); 

list.setAdapter(arrayAdapter); 

在列表视图的布局来自list_item_clickable.xml和textiview “txtitem” 里面:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtitem" 
     android:textColor="@android:color/white" 
     android:background="@android:drawable/editbox_dropdown_dark_frame" 
     android:textAlignment="center" /> 
</LinearLayout> 

这个结构在很多类中都有工作,并且像Textview所说的那样,textcolor总是白色的,但现在我需要根据文本的值(有时是白色,有时是红色)更改listview中的textcolor。那么,我怎样才能添加这个改变,而不会在代码的其余部分增加一个很大的影响长期的所有应用程序?

感谢您的任何建议!

回答

2

试试这个:

ArrayList<String> arrayList = new ArrayList<>(); 
list = (ListView) findViewById(R.id.list1); 

arrayAdapter = new ArrayAdapter<String>(INGR_NOTAS.this, R.layout.list_item_clickable, R.id.txtitem, arrayList){ 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     View view = super.getView(position, convertView, parent); 
     ((TextView)view.findViewById(R.id.txtitem)).setTextColor(position % 2 == 0 ? Color.WHITE : Color.RED); // here can be your logic 
     return view; 
    }; 
}; 

list.setAdapter(arrayAdapter); 

希望它有助于

+0

我试了一下,测试我是否可以更改代码中文本的颜色。我只是将Color.RED放在setTextColor中,但没有任何改变。 –

+0

是否向arrayList添加了字符串? – Oleg

+0

当我添加,取决于我使用的情况: –

1

创建您自己的适配器extends ArrayAdapter并覆盖getView()。这并不复杂。

1

使用textView.setTextColor(getResources().getColor(R.id.red);,如果基于阵列列表项。然后创建一个扩展为ArrayAdapter的类,在getView(,,)方法中设置颜色。

例如:

@Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder = null; 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.item1, null); 
       holder = new ViewHolder(); 
       holder.textView = (TextView)convertView.findViewById(R.id.text); 
holder.textView.setTextColor(getResources().getColor(R.id.your_color); 
       convertView.setTag(holder); 
      } 
      return convertView; 
     } 
0

随着SimpleAdapter它也是可能的。这是我的解决方案。

List<Map<String, String>> detailsList = debitorRecord.getDetails(); 

    String[] from = {"created_at","comment","sum"}; 
    int[] to = {R.id.detailCreatedAt, R.id.detailComment, R.id.detailSum}; 
    ListAdapter listAdapter = new SimpleAdapter(this, detailsList, R.layout.list_detail_item, from, to) 
    { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      TextView textView = (TextView) view.findViewById(R.id.detailSum); 
      //Do what u want here, in my case I 
      Map<String, String> currentRow = detailsList.get(position); 
      int type = Integer.parseInt(currentRow.get("type")); 
      if (DebitorRecord.TYPE_PAYMENT == type) textView.setTextColor(Color.GREEN); 
      else textView.setTextColor(Color.RED); 

      return view; 
     } 
    }; 

    listView.setAdapter(listAdapter);