我使用ListView在某些行中显示TextViews而不是其他行。我不知道会有多少项目,它运行正常,但当我向下滚动显示更改。
我正在开发一个用户填写报告和民意调查的应用。在一个活动中,我有一个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;
}
}
答案 0 :(得分:0)
我使用你的适配器制作了一个测试代码,但是使用了不同的视图/模型并尝试了但是它没有用。它只在我替换View row = view
之后才起作用;在View row = null;
方法的开头使用getView
,强制代码每次都会使视图膨胀。这就是我认为问题出现在item = (ProductHolder) row.getTag();
部分的原因。也许ProductHolder
模型不正确。