我有一个包含产品列表(如购物 list )的 Activity ,我们可以在其中选择产品单位的数量。
Activity 包含带有 TextWatcher 的 EditText 和带有个人适配器(扩展 BaseAdapter)的 ListView。
当我在 EditText 中键入一个键时,产品的 listView 会刷新为新数据。
但我想保留我选择的数据的状态。
例如,我在 EditText 中键入一个引用,然后 listView 包含 3 个数据(A、B、C),我选择 A,并键入它的 5 个单元,然后我在 EditText 中键入一个新引用(A B消失了)。
listView 仅包含 C,但我删除了搜索并返回到 A、B、C。
问题,产品 A 未被选择并且有 0 个单位(5 个单位已经消失)。
即使我在 EditText 中更改搜索,我也想保留所选的产品和单位。
请问怎么做?
这是一个代码 fragment :
Activity 的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+building_list/search_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Filter by reference"
android:inputType="textAutoCorrect|none|textAutoComplete|none|textNoSuggestions"
android:maxLines="1"
android:textSize="20dp" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:id="@+id/productListView">
</ListView>
</LinearLayout>
适配器:
public class AdapterProduct extends BaseAdapter {
private List<Product> lstProduct;
private LayoutInflater mInflater;
public AdapterProduct(Context context, List<Product> listP) {
lstProduct = listP;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
CheckedLinearLayout layoutItem;
if (convertView == null) {
layoutItem = (CheckedLinearLayout) mInflater.inflate(R.layout.row_product, parent, false);
final ViewHolderProduct viewHolder;
viewHolder = new ViewHolderProduct();
viewHolder.btPlusAmount = (Button)layoutItem.findViewById(R.id.btPlusAmount);
viewHolder.btPlusAmount.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Product p = (Product) viewHolder.product.getTag();
p.setAmount(p.getAmount()+1);
notifyDataSetChanged();
}
});
}
else {
layoutItem = (CheckedLinearLayout) convertView;
((ViewHolderProduct) layoutItem.getTag()).btPlusAmount.setTag(lstProduct.get(position));
}
ViewHolderProduct viewHolder = (ViewHolderProduct) layoutItem.getTag();
viewHolder.amount.setText(String.valueOf(lstProduct.get(position).getAmount()));
}
static class ViewHolderProduct {
protected TextView amount;
protected Button btPlusAmount;
}
}
以及带有 TextWatcher 的 AsyncTask:
AsyncTask
doInBackground =>
modelProduct = new AdapterProduct(leContext,
ProductJSON.getService().searchProducts(leContext, url, params[0])); //params[0] = the text of EditText
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//AsyncTask task
task.execute(s.toString());
}
};
最佳答案
我让你检查我的answer here .
1) 基本上,向您的适配器添加一个新属性。一个简单的 int[] 就足够了。存储里面选择的产品数量。
2) 每次构建 ListView
的一行 View 时,检查所选产品的数量。
3) 确保在单击按钮时更新所选产品的数量。此外,请确保在您的列表更改时调整/更新此数组。每次点击产品时调用 notifyDataSetChanged()。
关于java - 在 ListView 中更改数据时保持所选元素的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251149/