列表视图edittext数据在Android中滚动时丢失
Posted
技术标签:
【中文标题】列表视图edittext数据在Android中滚动时丢失【英文标题】:List view edittext data loses on scrolling in Android 【发布时间】:2015-08-12 05:54:41 【问题描述】:我希望具有 2 个编辑文本的列表视图即使在滚动后变得不可见也能保留这些值。当edittext失去焦点时,我希望将每个edittext值保存在一个arraylist中(2个arraylist - 一个用于数量,一个用于价格),稍后我可以将其保存到数据库中。 我尝试在 OntextChanegd 方法中添加代码,但它似乎不正确。
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;
【问题讨论】:
【参考方案1】:将监听器添加到ViewHolder
private class ViewHolder
TextView textView;
EditText editQty;
EditText editprice;
TextWatcher qtyWatcher;
TextWatcher priceWatcher;
int ref;
现在在 getView() 方法中删除 edittext 的现有监视程序,然后更新 edittext 中的文本并设置新的文本监视程序。
@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;
【讨论】:
考虑到滚动过程中发生的丢失,这会起作用吗? holder.editQty.setText(quantities.get(position)); holder.editprice.setText(itemPrices.get(position));在上面的代码中崩溃 更新帖子看看。因为我有编辑文本的初始化而出错。 @Abhijith 当我单击将我带到具有上述适配器的活动的按钮时崩溃。 java.lang.IndexOutOfBoundsException: 无效索引 0,大小为 0 有什么异常?【参考方案2】:您需要将更新值存储到数据数组中,因为列表使用回收视图。
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)
name.setValue(s);
@Override
public void afterTextChanged(Editable s)
);
【讨论】:
我应该在文本更改方法后这样做吗? 好的,没有。我刚刚树立了榜样,如何存储。您在文本更改方法中所做的 你能看看这个吗? ***.com/questions/30479562/…这就是我想做的事 根据问题,您只需要设置数据,因此视图将自动更改。关于在列表视图中维护编辑文本,最好打开对话框,用户将在其中输入值,然后您可以保存在您的数据中。以上是关于列表视图edittext数据在Android中滚动时丢失的主要内容,如果未能解决你的问题,请参考以下文章
在android中使用edittext对ListView进行排序
Android listview edittext过滤空间按钮?
在我的 android 应用程序中滚动列表视图时,数据正在消失。我在片段中使用列表视图
Android键盘将第一个EditText推送到AppBar下,并且仅可以向上滚动