我正在尝试使用listView
制作销售屏幕,其中每个listItem
都有一个editText
供用户输入特定项目的销售数量。我正在使用Array Adapter
生成listView。问题是,当我单击editText(例如,位置1)时,在其中键入内容并向下滚动,每第5个位置(位置5、9、13等)会集中注意力,并且键入的内容会自动重复在那些editTexts中。尝试了很多在线资源,但无法找出我在做什么错!预先感谢您的帮助。
class SalesEntryAdapter extends ArrayAdapter<SalesEntry> {
List<SalesEntry> mList;
public SalesEntryAdapter(List<SalesEntry> salesList) {
super(SalesActivity.this, R.layout.listview_sales_item, salesList);
this.mList = salesList;
}
class ViewHolder {
TextView tv_itemCode;
TextView tv_itemDescription;
TextView tv_priceLot;
TextView tv_tradePrice;
TextView tv_discPrice;
TextView tv_salePrice;
TextView tv_qtyOnHand;
TextView tv_lineTotal;
EditText et_saleQty;
int position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = getLayoutInflater();
double trade_price, net_price;
if(convertView == null){ // ensures re-usability of views created runtime
convertView = inflater.from(getContext()).inflate(R.layout.listview_sales_item, parent, false);
holder = new ViewHolder();
holder.tv_itemCode = convertView.findViewById(R.id.tv_itemCode);
holder.tv_itemDescription = convertView.findViewById(R.id.tv_itemDescription);
holder.tv_priceLot = convertView.findViewById(R.id.tv_priceLot);
holder.tv_tradePrice = convertView.findViewById(R.id.tv_tradePrice);
holder.tv_discPrice = convertView.findViewById(R.id.tv_discPrice);
holder.tv_salePrice = convertView.findViewById(R.id.tv_salePrice);
holder.tv_qtyOnHand = convertView.findViewById(R.id.tv_qtyOnHand);
holder.tv_lineTotal = convertView.findViewById(R.id.tv_lineTotal);
holder.et_saleQty = convertView.findViewById(R.id.et_saleQty);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
//convertView.setTag(holder);
}
SalesEntry salesEntry = mList.get(position);
holder.position = position; // save the position in the ViewHolder
holder.tv_itemCode.setText(salesEntry.get_item_code());
holder.tv_itemDescription.setText(salesEntry.get_item_desc());
holder.tv_priceLot.setText(salesEntry.get_price_lot());
holder.tv_tradePrice.setText(Double.toString(salesEntry.get_trade_price()));
holder.tv_discPrice.setText(Double.toString(salesEntry.get_net_price()));
holder.tv_salePrice.setText(Double.toString(salesEntry.get_net_price()));
holder.et_saleQty.setText(Integer.toString(salesEntry.get_sale_qty()));
//holder.et_saleQty.setId(position); // this is to set a reference ID for each EditText
trade_price = salesEntry.get_trade_price();
/*
holder.et_saleQty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
int position = v.getId();
EditText saleQty = (EditText) v;
//salesList.get(position).sale_qty = Integer.parseInt(saleQty.getText().toString());
}
}
});
*/
return convertView;
}
}