【问题标题】:Android ListView losing data when scrollingAndroid ListView在滚动时丢失数据
【发布时间】:2016-10-08 20:25:27
【问题描述】:

我正在使用 ListView 在某些行而不是其他行中显示 TextView。我不知道会有多少项目,它运行良好,但是当我向下滚动时,显示发生了变化。

我正在开发一个用户填写报告和投票的应用程序。在一个活动中,我有一个 ListView,其中子项包含 EditTexts、Checkbox 和 TextView。

EditTexts 和 Checkbox 的问题是在滚动时,它们拥有的内容会丢失。我可以通过将检查状态保存在数组中来解决 Checkbox 问题,但我无法解决 EditTexts 的问题。我知道有一些关于这个特定问题的问题,但他们提供的解决方案似乎不起作用。

我正在使用这个 BaseAdapter。

public class ProductAdapter extends BaseAdapter {

private Context mContext;
private List<Model> models = new ArrayList<>();
private ModelTable avModelTable = new ModelTable();
private BrandTable brandTable = new BrandTable();

public ProductAdapter(Context mContext, List<Model> models) {
    this.mContext = mContext;
    this.models = models;

}


@Override
public int getCount() {
    return models.size();
}

@Override
public Model getItem(int i) {
    return models.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}


@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    ProductHolder item = new ProductHolder();
    View row = view;
    if (row == null) {

        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        row = mInflater.inflate(R.layout.product_item, viewGroup, false);

        item.nameProduct = (TextView) row.findViewById(R.id.nameProduct);
        item.subCategoryProduct = (TextView) row.findViewById(R.id.subCategoryProduct);
        item.productGroup = (RadioGroup) row.findViewById(R.id.groupRadioProduct);
        item.oos_product = (RadioButton) row.findViewById(R.id.oos_product);
        item.av_product = (RadioButton) row.findViewById(R.id.av_product);
        item.shelfProduct = (EditText) row.findViewById(R.id.shelfProduct);
        item.quantityProduct = (EditText) row.findViewById(R.id.quantityProduct);
        item.priceProduct = (EditText) row.findViewById(R.id.priceProduct);
        item.layoutBrand = (LinearLayout) row.findViewById(R.id.layoutBrand);
        item.avBrand = (EditText) row.findViewById(R.id.avBrand);


        row.setTag(item);
    } else {
        item = (ProductHolder) row.getTag();
    }

    final Model model = models.get(i);

    item.nameProduct.setText(model.getProduct_name());
    item.subCategoryProduct.setText(brandTable.getBrandName(model.getBrand_id()));

    if (model.getQuantity() != 0) {
        item.quantityProduct.setText(String.valueOf(model.getQuantity()));
    }
    if (model.getShelf() != 0) {
        item.shelfProduct.setText(String.valueOf(model.getShelf()));
    }

    if (model.getPrice() != null) {
        item.priceProduct.setText(model.getPrice());
    }

    if (model.getBrand_id() == 1) {

        item.layoutBrand.setVisibility(View.GONE);
        item.avBrand.setVisibility(View.GONE);
        item.productGroup.setVisibility(View.VISIBLE);
        item.oos_product.setVisibility(View.VISIBLE);
        item.av_product.setVisibility(View.VISIBLE);

        switch (model.getAv()) {
            case 0:
                item.oos_product.setChecked(true);
                break;
            case 1:
                item.av_product.setChecked(true);
                break;
            default:
                break;
        }

        item.productGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.oos_product:
                        avModelTable.updateAv(model.getId(), 0);
                        break;
                    case R.id.av_product:
                        avModelTable.updateAv(model.getId(), 1);
                        break;
                    default:
                        break;
                }

            }

        });


    } else {


        item.layoutBrand.setVisibility(View.VISIBLE);
        item.avBrand.setVisibility(View.VISIBLE);
        item.productGroup.setVisibility(View.GONE);
        item.oos_product.setVisibility(View.GONE);
        item.av_product.setVisibility(View.GONE);


        item.avBrand.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                if (arg0.toString().equals("")) {
                    avModelTable.updateAv(model.getId(), 0);
                } else avModelTable.updateAv(model.getId(), Integer.parseInt(arg0.toString()));

            }
        });

    }


    item.shelfProduct.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            if (arg0.toString().equals("")) {
                avModelTable.updateShelf(model.getId(), 0);
            } else avModelTable.updateShelf(model.getId(), Integer.parseInt(arg0.toString()));

        }
    });

    item.quantityProduct.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            if (arg0.toString().equals("")) {
                avModelTable.updateQuantity(model.getId(), 0);
            } else
                avModelTable.updateQuantity(model.getId(), Integer.parseInt(arg0.toString()));

        }
    });

    item.priceProduct.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            if (arg0.toString().equals("")) {
                avModelTable.updatePrice(model.getId(), null);
            } else
                avModelTable.updatePrice(model.getId(), arg0.toString());

        }
    });


    return row;
}


private class ProductHolder {
    private TextView nameProduct, subCategoryProduct;
    private RadioButton oos_product, av_product;
    private RadioGroup productGroup;
    private EditText shelfProduct, quantityProduct, priceProduct, avBrand;
    private LinearLayout layoutBrand;

}

}

【问题讨论】:

  • 你有解决办法吗???我在这里遇到了同样的问题:(

标签: android listview adapter android-adapter baseadapter


【解决方案1】:

我使用您的适配器制作了一个测试代码,但使用了不同的视图/模型并尝试了它,但它不起作用。它只有在我替换 View row = view 后才有效;在 getView 方法的开头使用 View row = null;,强制代码每次都膨胀视图。这就是为什么我认为问题出在item = (ProductHolder) row.getTag(); 部分。也许ProductHolder 模型不正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多