ListView在视图中显示不正确的数据

时间:2013-04-05 22:42:25

标签: java android

如果我滚动浏览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;
}

更新 通过添加一个else语句解决它,将expiresIn专门设置为“”。如果我没有特别设定它,它会使用另一个项目的文本视图。

 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("");
    }

0 个答案:

没有答案