如果我滚动浏览我的 ListView,一个 ListView 项目中的一个 TextView (expiresIn) 会随机复制到其他项目中。我猜这与 ViewHolder 有关,但我真的看不出我哪里出错了,它只有 expiresIn TextView 我有问题:
public class MySyncListAdapter extends CouchbaseViewListAdapter {
protected MainActivity parent;
private CouchDbConnector db;
private static final String TAG = "MySyncListAdapter";
public MySyncListAdapter(MainActivity parent, CouchDbConnector couchDbConnector, ViewQuery viewQuery) {
super(couchDbConnector, viewQuery, true);
this.parent = parent;
this.db = couchDbConnector;
Log.d(TAG, "in constructor");
}
private static class ViewHolder {
ImageView img;
TextView label;
TextView dateAdded;
TextView expiresIn;
}
@Override
public View getView(int position, View itemView, ViewGroup parent) {
Log.d(TAG, "in getView");
View v = itemView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
ViewHolder vh = new ViewHolder();
vh.label = (TextView) v.findViewById(R.id.label);
vh.img = (ImageView) v.findViewById(R.id.img);
vh.dateAdded = (TextView) v.findViewById(R.id.tvDateAdded);
vh.expiresIn = (TextView) v.findViewById(R.id.tvExpiresInList);
v.setTag(vh);
}
TextView label = ((ViewHolder)v.getTag()).label;
TextView dateAdded = ((ViewHolder)v.getTag()).dateAdded;
TextView expiresIn = ((ViewHolder)v.getTag()).expiresIn;
ImageView img = ((ViewHolder)v.getTag()).img;
Row row = getRow(position);
JsonNode item = row.getValueAsNode();
JsonNode itemText = item.get("header");
JsonNode dateAddedText = item.get("dateAdded");
JsonNode expiryDateText = item.get("expiryDate");
JsonNode attachmentText = item.get("_attachments");
ViewHolder holder = (ViewHolder) v.getTag();
if(label != null) {
holder.label.setText(itemText.getTextValue());
}
if(dateAdded != null) {
Log.d(TAG, "in dateAdded getView");
if(CompareDate.isDateAddedToday(dateAddedText.getTextValue())){
holder.dateAdded.setText(R.string.today);
}
else if (CompareDate.isDateAddedYesterday(dateAddedText.getTextValue())){
holder.dateAdded.setText(R.string.yesterday);
}
else{
String age = String.valueOf(CompareDate.getAgeInDays(dateAddedText.getTextValue()));
holder.dateAdded.setText( age + " " + parent.getResources().getString(R.string.days_ago));
}
}
if(!expiryDateText.isNull() && expiresIn != null){
long noDays = CompareDate.getDaysUntil(expiryDateText.getTextValue());
if(noDays<6){
holder.expiresIn.setTextColor(Color.parseColor("#ff4444"));
}
holder.expiresIn.setText(String.valueOf(noDays) + " " + parent.getResources().getString(R.string.days_until_expiry));
}
if(img != null){
//bitmap stuff
holder.img.setImageBitmap(bitmap);
}
return v;
}
更新 通过添加一个专门设置 expiresIn 到“”的 else 语句来解决它。希望解释一下,但是为什么如果我不专门设置它,它会使用另一个项目的文本视图。
if(!expiryDateText.isNull() && expiresIn != null){
long noDays = CompareDate.getDaysUntil(expiryDateText.getTextValue());
if(noDays<6){
holder.expiresIn.setTextColor(Color.parseColor("#ff4444"));
}
holder.expiresIn.setText(String.valueOf(noDays) + " " + parent.getResources().getString(R.string.days_until_expiry));
}
else {
holder.expiresIn.setText("");
}