简体   繁体   English

ListView在滚动时更改记录

[英]ListView changes records while scrolling

This is how I see my screen the when the page loads. 这是我在页面加载时看到屏幕的方式。 But when I start scrolling through the list, all my records start to change and it becomes really weird. 但是,当我开始滚动列表时,我的所有记录都开始更改,并且变得很奇怪。 I really don't know why, any suggestions? 我真的不知道为什么,有什么建议吗?

原版的

This is the screen after I scroll through my ListView up and down. 这是我上下滚动ListView后的屏幕。 滚动列表后

Adapter: 适配器:

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;
    }
}

Something that worked for me was this: 对我有用的是:

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

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

But this solution made the navigation much slower. 但是此解决方案使导航速度大大降低。

EDIT 编辑

This was not the correct solution. 这不是正确的解决方案。

The way to go was to clean all views before assigning the new values. 解决方法是在分配新值之前清除所有视图。

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
}

Android ListView recycles views. Android ListView回收视图。 At the moment the problem is that your adapter always appends new data to the TextViews. 目前的问题是您的适配器始终将新数据追加到TextViews。 Now every time a new row is shown, you are appending the data from the new row to the old data. 现在,每次显示新行时,您都将数据从新行追加到旧数据。

To solve this, you should clear the TextViews from the old data if you are using the convertView parameter. 要解决此问题,如果使用convertView参数,则应从旧数据中清除TextViews。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM