ListView在滚动时更改记录

时间:2012-05-08 11:17:42

标签: android android-listview records

这是我在屏幕加载时看到的方式。但是当我开始滚动列表时,我的所有记录都开始变化,变得非常奇怪。我真的不知道为什么,有什么建议吗?

Original

这是我向上和向下滚动ListView后的屏幕。 After scrolling the list

适配器:

private class TechnicalDocumentationAdapter extends
        ArrayAdapter<TechnicalDocumentation> {

    private ArrayList<TechnicalDocumentation> items;

    public TechnicalDocumentationAdapter(Context context,
            int textViewResourceId, ArrayList<TechnicalDocumentation> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.technicaldocumentationrow, null);
        }
        TechnicalDocumentation technicalDocumentation = items.get(position);
        if (technicalDocumentation != null) {
            TextView tt = (TextView) v.findViewById(R.id.TDRdate);
            TextView bt = (TextView) v.findViewById(R.id.TDRobjectTypeCode);
            TextView ct = (TextView) v
                    .findViewById(R.id.TDRperformedAction);
            TextView at = (TextView) v.findViewById(R.id.TDRobjectName);

            tt.setText(tt.getText() + "\n " + technicalDocumentation.date);
            bt.setText(bt.getText() + "\n "
                    + technicalDocumentation.objectTypeCode);
            ct.setText(ct.getText() + "\n "
                    + technicalDocumentation.performedAction);
            at.setText(at.getText() + "\n "
                    + technicalDocumentation.objectName);

        }
        return v;
    }
}

2 个答案:

答案 0 :(得分:10)

对我有用的是:

View row = convertView;
ComponentsHolder holder = null;
**row = null;** // Added this line to fix the problem. 

if (row == null) {...}

但是这个解决方案使导航速度慢得多。

修改

这不是正确的解决方案。

要做的就是在分配新值之前清理所有视图。

if (row == null) {

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ComponentsHolder();
holder.id = (TextView) row.findViewById(R.id.id);
holder.name = (TextView) row.findViewById(R.id.name);
holder.description = (TextView) row.findViewById(R.id.description);
holder.price = (TextView) row.findViewById(R.id.price);
holder.photo = (ImageView) row.findViewById(R.id.photo);
holder.add = (Button) row.findViewById(R.id.btnAddDish);
row.setTag(holder);
} else {
holder = (ComponentsHolder) row.getTag();
holder.id.setText("");
holder.name.setText("");
holder.description.setText("");
holder.price.setText("");
holder.photo.setVisibility(View.GONE); //This was the real problem, if the image was not empty or gone, the view gone crazy
}

答案 1 :(得分:3)

Android ListView回收视图。目前问题是您的适配器始终将新数据附加到TextViews。现在,每次显示新行时,您都会将新行中的数据附加到旧数据。

要解决此问题,如果使用convertView参数,则应从旧数据中清除TextView。