我正在开发一个order-taking
应用,其中用户根据他/她需要的产品输入数量。它们是listview中针对每个产品的editText
字段。
我面临的问题是,当用户在listview
的第3行输入数量时,他/她输入的数量每10行重复一次。
我正在添加名为 nextValueOf
的arraylist中的所有产品,并将其发送到下一个活动。请查看代码并告诉我我在哪里做错了
public class TweetListAdaptor extends ArrayAdapter<Medicine> implements Filterable {
private ArrayList<Medicine> mOriginalValues = new ArrayList<Medicine>(); // Original Values
private ArrayList<Medicine> mDisplayedValues = new ArrayList<Medicine>(); // Values to be displayed
LayoutInflater inflater;
public TweetListAdaptor(Context context,ArrayList<Medicine> products) {
super(context, R.layout.medicine_list_info, products);
this.mOriginalValues = products;
this.mDisplayedValues = products;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mDisplayedValues.size();
}
@Override
public Medicine getItem(int position)
{
return mDisplayedValues.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
private TextView productName;
private TextView productType;
private EditText productQuantity;
public ViewHolder(View v) {
this.productName = (TextView) v.findViewById(R.id.textViewProductID);
this.productType = (TextView) v.findViewById(R.id.textViewProductName);
this.productQuantity= (EditText) v.findViewById(R.id.editTextEnterQuantity);
// this.date = (TextView) v.findViewById(R.id.date);
// this.status = (TextView) v.findViewById(R.id.staus);
}
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.medicine_list_info, null);
holder = new ViewHolder(v);
v.setTag(holder);
}else {
holder = (ViewHolder) v.getTag();
}
final Medicine o = getItem(position);
if (o != null) {
holder.productName.setText(o._product_name);
NumberFormat nm = NumberFormat.getNumberInstance();
holder.productType.setText(nm.format(o._price));
holder.productQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
final EditText Caption = (EditText) v;
Caption.setFocusable(true);
holder.productQuantity.setFocusable(true);
try{
o._quantity = Integer.parseInt(Caption.getText().toString());
}catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
if(o._quantity != 0 ){
Medicine olx = new Medicine(o._id,o._product_Code,o._product_name,o._price, o._quantity );
String ods = "ID: "+ o.get_id() +"Name :" + o.get_product_name() +" ,Price: " + o.get_price() + " ,Quantity: " + o.get_quantity(); ;
Log.d("Problem Is here :", ods);
nextValuesOf.add(olx);
}
}
}
});
}
return v;
}
答案 0 :(得分:0)
这是因为ListView
回收了其他子项目的相同视图。
您需要更改适配器以说明列表视图,您的项目视图是不同的视图类型,否则您将在EditText
的其他列表项中看到输入的文本。
Override
getViewTypeCount
返回项目计数
public int getViewTypeCount() {
return getCount();
}
并且还更改getItemViewType以返回唯一ID,例如,position是唯一编号
public int getItemViewType(int position) {
return position;
}