我正在根据其中一个子项的文本值设置行的背景颜色。但是,无论其文本值如何,都会设置多个背景。上下滚动时会变得更糟。适配器的代码:
package com.test.app;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private LayoutInflater mInflater = null;
private ArrayList<String[]> mItems = new ArrayList<String[]>();
public MyBaseAdapter(Context context, ArrayList<String[]> items) {
mItems = items;
mInflater = LayoutInflater.from(context);
}
public void addItem(String[] it) {
mItems.add(it);
}
public void setListItems(ArrayList<String[]> lit) {
mItems = lit;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int position) {
return mItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
static class ViewHolder {
public TextView tv0,tv1;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = null;
ViewHolder viewHolder;
if(convertView == null)
{
rowView = mInflater.inflate(R.layout.history_row, null);
}
else
{
rowView = convertView;
}
viewHolder = new ViewHolder();
viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0);
viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1);
rowView.setTag(viewHolder);
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.tv0.setText(mItems.get(position)[0].toString());
holder.tv1.setText(mItems.get(position)[1].toString());
if(holder.tv1.getText().equals("0"))
{
rowView.setBackgroundColor(0xAA777777);
// Only the row containing "0" should be colored, yet it colors multiple, random rows.
}
return rowView;
}
}