我有一个项目列表如下:
private void getProducts() {
List<Product> listProducts = Product.getListProduct(true);
if (listProducts != null && listProducts.size() > 0) {
ProductAdapter m_adapterProduct = new ProductAdapter(this, listProducts);
mlvwProducts.setAdapter(m_adapterProduct);
}
}
ProductAdapter类如下:
public class ProductAdapter extends ArrayAdapter<Product> {
private Context context;
private List<Product> mdata;
public ProductAdapter(Context context, List<Product> mdata) {
super(context, R.layout.act_item_product_adapter, mdata);
this.context = context;
this.mdata = mdata;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = LayoutInflater.from(context).inflate(R.layout.act_item_product_adapter,null);
TextView m_product_name = (TextView) item.findViewById(R.id.adapter_product_nameProduct);
TextView m_product_cost = (TextView) item.findViewById(R.id.adapter_product_cost);
TextView m_product_stock = (TextView) item.findViewById(R.id.adapter_product_stock);
EditText m_product_amount = (EditText) item.findViewById(R.id.adapter_product_amount);
ImageView m_product_photo = (ImageView) item.findViewById(R.id.adapter_product_photo);
m_product_name.setText(datos.get(position).getNombreProducto());
m_product_cost.setText(" Cost: " + String.format("%.2f",mdata.get(position).getCostSold()));
m_product_stock.setText(" Stock: " + Integer.toString(mdata.get(position).getStock()));
//m_product_amout.setText("0");
if (datos.get(position).getimagenProduct() != null){
byte[] decodedString = Base64.decode(mdata.get(position).getimagenProduct(), Base64.DEFAULT);
m_product_photo.setImageBitmap(BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length));
}
return item;
}
}
一切都运行良好,我可以检索产品的信息并在列表中显示它们,但是当在EditText(m_product_amount)中输入一个值并单击活动的另一个对象时,输入的数字将被删除并保留在0处是布局中的默认数字。
我做错了什么?
欢迎任何想法或建议
由于
=============问题更新===============
act_item_product_adapter.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/adapter_product_photo"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_toRightOf="@id/adapter_product_photo"
android:id="@+id/linearLayout2">
<TextView
android:id="@+id/adapter_product_nameProducto"
android:layout_width = "match_parent"
android:layout_height = "37dp"
android:gravity="center"
android:textSize="20dp"
android:text="Name of product"/>
</LinearLayout>
<LinearLayout
android:layout_width= "170dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout3"
android:layout_below="@+id/linearLayout2"
android:layout_toRightOf="@+id/adapter_product_photo"
android:layout_alignBottom="@+id/adapter_product_photo">
<TextView
android:id="@+id/adapter_product_cost"
android:layout_width = "170dp"
android:layout_height = "wrap_content"
android:text="cost"/>
<TextView
android:id="@+id/adapter_product_stock"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:text="stock"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="@+id/linearLayout2"
android:layout_toEndOf="@+id/linearLayout3"
android:layout_toRightOf="@+id/linearLayout3"
android:orientation="horizontal"
android:layout_alignBottom="@+id/linearLayout3">
<TextView
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:gravity="center"
android:textSize="20dp"
android:text="Amount"/>
<EditText
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:inputType="number"
android:id="@+id/adapter_product_amount"
android:gravity="center"
android:text="0"/>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
我怀疑ListView没有显示多个项目,它是一个单独的项目,在其中显示它包含的对象的临时信息(几毫秒),然后显示下一个相同但具有其他值的项目
正是由于这个原因,当指向另一部分时,信息被删除,而在公共视图中分配的信息则是getView。
然后我根据项目编号为EditText的值创建一个类型字符串数组,如下所示:
public class ProductAdapter extends ArrayAdapter<Producto> {
private Context context;
private List<Product> mdata;
private EditText m_product_amount;
private String[] m_amounts;
public ProductAdapter(Context context, List<Product> mdata) {
super(context, R.layout.act_item_product_adapter, mdata);
this.context = context;
this.mdata = mdata;
m_amounts = new String[mdata.size()];
}
...
}
&#13;
然后我使用数组中包含的值初始化editText,如下所示:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = LayoutInflater.from(context).inflate(R.layout.act_item_product_adapter,null);
....
if (null != m_amounts[position]) m_product_amount.setText(m_amounts[position]);
....
return item;
}
&#13;
最后要更新数组中每个元素的值,我会执行以下操作:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
....
m_product_amount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.adapter_product_amount:{
if (false == hasFocus && m_product_amount.getText().length() > 0) {
m_amounts[position] = m_product_amount.getText().toString();
}
}
}
}
});
...
return item;
}
&#13;