问题描述
我有产品的列表(如购物清单),在此我们可以选择的产品单元数目的活动。
该活动包含的EditText与TextWatcher和个人适配器一个ListView(延伸BaseAdapter)。
当我键入的EditText的关键,产品的ListView的刷新与新的数据。
但我想保持我的选择的数据的状态。
例如,我键入的EditText的引用,则列表视图中包含3 DATAS(A,B,C),我选择A,和类型5个单位的话,那么我键入的EditText(A一个新的参考和B消失)。
在ListView只包含C,但我删除我的搜索,并返回到A,B,C。
问题,产品A为未选中,并有单位0(5个单位已经消失)。
我想保持选定的产品和单位即使我改变我的搜索的EditText
该怎么做吗?
下面是一个code片断:
活动的布局:
<?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> <的EditText
机器人:ID =@ + building_list / search_box
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:提示=过滤器引用
安卓的inputType =textAutoCorrect |无| textAutoComplete |无| textNoSuggestions
机器人:MAXLINES =1
机器人:TEXTSIZE =20dp/> < ListView控件
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_marginLeft =10dp
机器人:ID =@ + ID / productListView>
< /&的ListView GT;
< / LinearLayout中>
该适配器:
公共类AdapterProduct延伸BaseAdapter {
私人列表<产品与GT; lstProduct;
私人LayoutInflater mInflater; 公共AdapterProduct(上下文的背景下,列表与LT;产品> listP){
lstProduct = listP;
mInflater = LayoutInflater.from(上下文);
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
CheckedLinearLayout layoutItem; 如果(convertView == NULL){
layoutItem =(CheckedLinearLayout)mInflater.inflate(R.layout.row_product,父母,假);
最后ViewHolderProduct viewHolder;
viewHolder =新ViewHolderProduct(); viewHolder.btPlusAmount =(按钮)layoutItem.findViewById(R.id.btPlusAmount);
viewHolder.btPlusAmount.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
产品p =(产物)viewHolder.product.getTag();
p.setAmount(p.getAmount()+ 1);
notifyDataSetChanged();
}
});
}
其他{
layoutItem =(CheckedLinearLayout)convertView;
((ViewHolderProduct)layoutItem.getTag())btPlusAmount.setTag(lstProduct.get(位置))。
} ViewHolderProduct viewHolder =(ViewHolderProduct)layoutItem.getTag();
viewHolder.amount.setText(将String.valueOf(lstProduct.get(位置).getAmount()));
}静态类ViewHolderProduct {
保护TextView的金额;
保护按钮btPlusAmount;
}
}
和与TextWatcher的AsyncTask的:
的AsyncTask
doInBackground =>
modelProduct =新AdapterProduct(leContext,
。ProductJSON.getService()searchProducts(leContext,网址,则params [0])); // PARAMS [0] =的EditText文
私人TextWatcher filterTextWatcher =新TextWatcher(){ 公共无效afterTextChanged(编辑S){ } 公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
INT后){
} 公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
诠释计数){ // AsyncTask的任务
task.execute(s.toString());
}
};
我让你看看我。
1)基本上,添加一个新的属性适配器。一个简单的INT []就足够了。存储的内选定的产品数量。
2)每次你建立的一排查看你的的ListView
,检查产品的数量选择。
3)请务必及时更新的,当你点击按钮来选择产品的数量。此外,请务必调整/更新该阵列时,您的名单变化。呼叫notifyDataSetChanged()点击一个产品各一次。
I have an activity with a list of Products (like a shopping list), in which we can choose the number of product units.
The activity contains EditText with TextWatcher and a ListView with a personal Adapter (extends BaseAdapter).
When I type a key in the EditText, the listView of products is refreshed with the new data.
But I would like to keep the state of my selected data.
For example, I type a reference in the EditText, then the listView contains 3 datas (A, B, C), I choose A, and type 5 units of it, then I type a new reference in the EditText (A and B disappears).
The listView contain only C, but I remove my search and go back to A, B, C.
the problem, the product A is unselected and have 0 of units (the 5 units have disappeared).
I would like to keep the selected products and the units even if I change my search in the EditText.
How to do that please ?
Here is a code snippet :
The layout of the 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>
The Adapter :
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;
}
}
And the AsyncTask with TextWatcher :
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());
}
};
I let you check my answer here.
1) Basically, add a new attribute to your adapter. A simple int[] is enough. Store the number of products selected inside.
2) Each time you build the View of a row of your ListView
, check the number of products selected.
3)Make sure to update the number of products selected when you click on the button. Also, make sure to resize/update this array when your List changes. Call notifyDataSetChanged() Each Time you click on a product.
这篇关于保留所选元素的状态,当数据的ListView正在改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!