我的ListView
每行都有一个EditText
。当我在EditText
中输入值时,它会在ListView
的每四行中复制一次。我怎样才能防止这种情况。
public class ItemsAdapter extends ArrayAdapter<Items>{
Double Price;
Double Quantity;
String _Quantity;
private int resource;
private LayoutInflater inflater;
private Context context;
Items items;
public ItemsAdapter(Context ctx, int resourceId, List<Items> objects){
super( ctx, resourceId, objects );
resource = resourceId;
inflater = LayoutInflater.from( ctx );
context=ctx;
}
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
ItemViewHolder viewHolder;
items = getItem( position );
if (convertView == null) {
viewHolder=new ItemViewHolder();
convertView = ( RelativeLayout ) inflater.inflate( resource, null );
viewHolder.txtQuantity =(EditText)convertView.findViewById(R.id.txtQuantity);
viewHolder.lblItemsName=(TextView) convertView.findViewById(R.id.lblItemName);
viewHolder.lblPrice = (TextView) convertView.findViewById(R.id.lblPrice);
} else {
viewHolder = (ItemViewHolder) convertView.getTag();
}
viewHolder.lblItemsName.setText(items.getItemName());
viewHolder.lblPrice.setText(items.getPrice().toString());
return convertView;
}
public class ItemViewHolder {
public EditText txtQuantity ;
public TextView lblItemsName;
public TextView lblPrice;
}
}`
答案 0 :(得分:0)
当您开始在ListView
的每个单元格中输入数量时,您应该将数量保存到items
之后,您从item
... your above code
}
viewHolder.txtQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {
items.setQuantity(s); // set quantity for each item when you enter quantity
}
});
viewHolder.lblItemsName.setText(items.getItemName());
viewHolder.lblPrice.setText(items.getPrice().toString());
viewHolder.txtQuantity.setText(items.getQuantity()); // load quantity for each item
...
希望这个帮助
答案 1 :(得分:0)
发生重复是因为滚动时视图会在Listview
中被回收。为避免这种情况,您应该使用Edittext
方法重置getView()
中的文字。
如果你想再次回到视图时保留EditText
中的文字,你应该做像PhanVănLinh建议的那样。
答案 2 :(得分:0)
我明白了
public ItemAdapter(Context context, int textViewResourceId,
ArrayList<Cart> productList) {
super(context, textViewResourceId, productList);
this.productList = new ArrayList<Cart>();
this.productList.addAll(productList);
}
@Override
public int getCount() {
return this.productList.size();
};
@Override
public View getView(final int position, View view, ViewGroup parent) {
DecimalFormat df = new DecimalFormat("0.00##");
// productList= DBAdapter.getCartList();
final Cart product = productList.get(position);
if (view == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_cart, null);
EditText quantity = (EditText) view.findViewById(R.id.txtQuantity);
//attach the TextWatcher listener to the EditText
quantity.addTextChangedListener(new MyTextWatcher(view));
}
TextView ItemNumber=(TextView)view.findViewById(R.id.itemNumber);
ItemNumber.setVisibility(View.GONE);
EditText quantity = (EditText) view.findViewById(R.id.txtQuantity);
quantity.setTag(product);
if(product.getQuantity() != 0){
quantity.setText(String.valueOf(product.getQuantity()));
}
else {
quantity.setText("1");
}
TextView itemNumber = (TextView) view.findViewById(R.id.itemNumber);
itemNumber.setText(product.getItemNumber());
TextView ItemName = (TextView) view.findViewById(R.id.lblItemName);
ItemName.setText(product.getItemName());
TextView price = (TextView) view.findViewById(R.id.lblPrice);
price.setText( df.format(product.getPrice()));
TextView ext = (TextView) view.findViewById(R.id.lblTotal);
if(product.getQuantity() != 0){
ext.setText("$" + df.format(product.getTotal()));
}
else {
ext.setText("");
}
return view;
}
}
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do nothing
}
public void afterTextChanged(Editable s) {
DecimalFormat df = new DecimalFormat("0.00##");
String qtyString = s.toString().trim();
int quantity = qtyString.equals("") ? 0:Integer.valueOf(qtyString);
EditText qtyView = (EditText) view.findViewById(R.id.txtQuantity);
Cart product = (Cart) qtyView.getTag();
if(product.getQuantity() != quantity){
Double currPrice = product.getTotal();
Double extPrice = quantity * product.getPrice();
Double priceDiff = Double.valueOf(df.format(extPrice - currPrice));
product.setQuantity(quantity);
product.setTotal(extPrice);
TextView ext = (TextView) view.findViewById(R.id.lblTotal);
if(product.getQuantity() != 0){
ext.setText("$" + df.format(product.getTotal()));
}
else {
ext.setText("");
}
if(product.getQuantity() != 0){
qtyView.setText(String.valueOf(product.getQuantity()));
}
else {
qtyView.setText("");
}
orderTotal += priceDiff;
TextView cartTotal = (TextView) findViewById(R.id.cartTotal);
cartTotal.setText(df.format(orderTotal));
}
return;
}
}