android - ListView 滚动时改变记录

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

最佳答案

对我有用的是:

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

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

但是这个解决方案让导航变慢了很多。

编辑

这不是正确的解决方案。

要走的路是在分配新值之前清理所有 View 。

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 滚动时改变记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10497559/

相关文章:

java - 谷歌日历 API : Updating Reminders to an event

android - 'play'按钮外观

android widget listview 可点击

android - 快速上下滚动 ListView 会更改 ListView 项的顺序

MySQL DELETE 除最新 X 记录外的所有记录

android - 使用 Google Play 服务位置在 android 中唤醒过多的警报管理器

android - 如何在不使用相机应用程序的情况下(通过服务)从相机中检索图像

android - 在android中下拉列表菜单

mysql - 如何从mysql表中删除重复记录但有1条记录

sql-server - MSSQL记录日期/时间自动删除