您好我试图在我的列表视图的单元格中更改文本颜色,但我不能因为我得到
11-25 20:22:37.176: E/AndroidRuntime(1338): FATAL EXCEPTION: Thread-116
11-25 20:22:37.176: E/AndroidRuntime(1338): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-25 20:22:37.176: E/AndroidRuntime(1338): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-25 20:22:37.176: E/AndroidRuntime(1338): at java.util.ArrayList.get(ArrayList.java:304)
11-25 20:22:37.176: E/AndroidRuntime(1338): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
11-25 20:22:37.176: E/AndroidRuntime(1338): at morisson.Notowaniagieldowe.Tab2Fragment.getViewByPosition(Tab2Fragment.java:254)
11-25 20:22:37.176: E/AndroidRuntime(1338): at morisson.Notowaniagieldowe.Tab2Fragment$1.run(Tab2Fragment.java:123)
这是我试图改变颜色的部分代码:
int k = cash.size();
if(k>0){
for(int l=0;l<k;l++){
LinearLayout root = (LinearLayout) getViewByPosition(l+1,listView);
if(price<m)
{
((TextView) root.findViewById(R.id.amount)).setTextColor(Color.RED);
}
}
}
当我创建linearlayout时,我必须增加l + 1,因为如果我从零开始,我会改变标题处的颜色。这是我用来更改返回视图的方法:
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
更新: 这是我的适配器。我想补充一点,当我的应用程序启动时,列表为空,但更新颜色的新线程正在运行。
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
super(activity, R.layout.lista_wlasnych_spolek, list);
this.activity=activity;
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public HashMap<String, String> getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private int mColor = Color.BLACK;
public void setColor(int color)
{
mColor = color;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ListViewHolder();
convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ListViewHolder) convertView.getTag();
}
HashMap<String, String> map=list.get(position);
listViewHolder.txtFirst.setText(map.get(FIRST_COLUMN));
listViewHolder.txtSecond.setText(map.get(SECOND_COLUMN));
listViewHolder.txtThird.setText(map.get(THIRD_COLUMN));
listViewHolder.txtFourth.setText(map.get(FOURTH_COLUMN));
TextView textViewFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
TextView textViewSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
TextView textViewThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
TextView textViewFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
textViewFirst.setTextColor(mColor);
textViewSecond.setTextColor(mColor);
textViewThird.setTextColor(mColor);
textViewFourth.setTextColor(mColor);
return convertView;
}
public class ListViewHolder {
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
}
答案 0 :(得分:1)
您不应该管理适配器之外的视图,因为当ListView将绘制视图或每个视图都可见时,您没有任何控制权。
您应该在适配器中设置颜色并调用notifyDataSetChanged以通知ListView重绘视图(为每个可见项调用getView)。
基本上你已经使用你的适配器的变量mColor来做了,你只需要在更改颜色后添加对notifyDataSetChanged的调用,以通知ListView重绘视图。