问题描述
我想即使有隐形变得滚动之后的2至edittexts preserve值列表视图。而当亏损的EditText重点,我希望每个的EditText值保存在一个数组列表(ArrayList的2 - 一个用于分位数,一个是价格),我以后可以保存到数据库中。
我试过有在OntextChanegd方法code,但它似乎不正确的。
I want the listview having 2 edittexts to preserve the values even after becoming invisible after scrolling. And when the edittext losses focus, I want each edittext value to be saved in a arraylist (2 arraylist - one for quantites and one for prices), which I can later save to the database.I tried having the code in the OntextChanegd method, but it doesn't seem right.
public class CustomAdapter extends BaseAdapter {
ArrayList<String> names;
Context context;
ArrayList<String> itemPrices = new ArrayList<>();
ArrayList<String> quantities = new ArrayList<>();
CustomAdapter(ArrayList v, Context c) {
names = v;
context = c;
}
@Override
public int getCount() {
return names.size();
}
@Override
public Object getItem(int position) {
return names.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
try {
final ViewHolder holder;
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
holder.textView = (TextView) convertView.findViewById(R.id.rowText);
holder.editQty = (EditText) convertView.findViewById(R.id.qty);
holder.editprice = (EditText) convertView.findViewById(R.id.price);
holder.textView.setTextSize(20);
holder.textView.setTextColor(Color.BLACK);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView.setText(names.get(position));
holder.editQty.setHint("Quantity");
holder.editprice.setHint("Price");
holder.editQty.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
holder.editprice.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}catch (NumberFormatException ex){
Toast.makeText(context,"!!!",Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
return convertView;
}
private class ViewHolder {
TextView textView;
EditText editQty;
EditText editprice;
int ref;
}
}
推荐答案
听众添加到 ViewHolder
private class ViewHolder {
TextView textView;
EditText editQty;
EditText editprice;
TextWatcher qtyWatcher;
TextWatcher priceWatcher;
int ref;
}
现在在getView()方法去除的EditText现有的手表,然后更新中的EditText文字和设置新的文本观察家。
Now in the getView() method remove the existing watches of the edittext , then update the text in the edittext and set the new text watchers.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
try {
final ViewHolder holder;
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
holder.textView = (TextView) convertView.findViewById(R.id.rowText);
holder.editQty = (EditText) convertView.findViewById(R.id.qty);
holder.editprice = (EditText) convertView.findViewById(R.id.price);
holder.textView.setTextSize(20);
holder.textView.setTextColor(Color.BLACK);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
if (holder.qtyWatcher != null) {
holder.editQty.removeTextChangedListener(holder.qtyWatcher);
}
if (holder.priceWatcher != null) {
holder.editprice.removeTextChangedListener(holder.priceWatcher);
}
holder.textView.setText(names.get(position));
holder.editQty.setHint("Quantity");
holder.editprice.setHint("Price");
if(position<quantities.size())
holder.editQty.setText(quantities.get(position));
else
holder.editQty.setText("");
if(position<itemPrices.size())
holder.editprice.setText(itemPrices.get(position));
else
holder.editprice.setText("");
holder.qtyWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//Update the quantity
}
};
holder.editQty.addTextChangedListener(holder.qtyWatcher);
holder.priceWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//Update you price
}
};
holder.editprice.addTextChangedListener(holder.priceWatcher);
return convertView;
} catch (NumberFormatException ex) {
Toast.makeText(context, "!!!", Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
return convertView;
}
这篇关于在Android的滚动列表视图的EditText数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!