我有下一个结构:
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);
listview的布局来自list_item_clickable.xml和textiview&#34; txtitem&#34;在里面:
<?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。那么,我怎样才能添加这个更改而不会在其余的代码中添加很大的影响很长的所有App ????
感谢您的任何建议!!
答案 0 :(得分: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);
希望有所帮助
答案 1 :(得分:1)
创建自己的getView()
并覆盖Select count(*)
from call_history
where system_disposition = 'CONNECTED' and call_type = 'OUTBOUND'
的适配器。它并不复杂。
答案 2 :(得分: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;
}
答案 3 :(得分: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);