Java 在ListView中更改数据时保持选定元素的状态
我有一个活动,有一个产品列表(比如购物清单),我们可以在其中选择产品单元的数量 该活动包含带有TextWatcher的EditText和带有个人适配器(extends BaseAdapter)的ListView 当我在EditText中键入一个键时,产品的listView将用新数据刷新 但我希望保留所选数据的状态 例如,我在EditText中键入一个引用,然后listView包含3个数据(a、B、C),我选择a并键入5个单位,然后在EditText中键入一个新引用(a和B消失) listView只包含C,但我删除了搜索并返回到A、B、C 问题是,产品A未被选中,并且有0个单位(5个单位已消失) 即使我在编辑文本中更改搜索,我也希望保留选定的产品和单位 请问怎么做 以下是一段代码片段: 活动的布局:Java 在ListView中更改数据时保持选定元素的状态,java,android,android-listview,state,Java,Android,Android Listview,State,我有一个活动,有一个产品列表(比如购物清单),我们可以在其中选择产品单元的数量 该活动包含带有TextWatcher的EditText和带有个人适配器(extends BaseAdapter)的ListView 当我在EditText中键入一个键时,产品的listView将用新数据刷新 但我希望保留所选数据的状态 例如,我在EditText中键入一个引用,然后listView包含3个数据(a、B、C),我选择a并键入5个单位,然后在EditText中键入一个新引用(a和B消失) listView
<?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>
我让你检查一下我的电脑
1) 基本上,向适配器添加一个新属性。一个简单的int[]就足够了。存储内部选定的产品数量
2) 每次构建列表视图的一行视图时,请检查所选产品的数量
3) 确保在单击按钮时更新所选产品的数量。此外,请确保在列表更改时调整/更新此数组。每次单击产品时调用notifyDataSetChanged()。1。从列表视图保存所选位置。2.使用该职位获得所选的产品。3.保存该产品(已选定)4。从编辑文本中删除所有文本或使用新数据刷新列表视图时,如果产品保存在列表中,则选择该位置的助理(使用步骤3中的“保存选定位置”)。。。。。这很简单。谢谢你,我会尝试一下,我会回答你,以便结束这篇文章或提出新问题;)好的,然后我有一个包含所选产品的ArrayList,在适配器的构造函数上,当新数据到来时,我更新产品的数量。在notifyDataSetChanged之前,我更新所选产品的ArrayList(以静态方式访问)。对我来说没关系,非常感谢!
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;
}
}
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());
}
};