ListView为啥滚动listView时edittext数据丢失
Posted
技术标签:
【中文标题】ListView为啥滚动listView时edittext数据丢失【英文标题】:ListView Why edittext data lost when scroll listViewListView为什么滚动listView时edittext数据丢失 【发布时间】:2014-10-06 11:19:16 【问题描述】:我在几页中遵循了一些代码,但它不起作用。这是我的 pageAdapter 代码:
public class ViewHolder
int ref;
TextView txtTimeToEat;
LinearLayout lnlDishListView, lnlDishToGetpos;
ImageView imgDish;
TextView txtDishName;
TextView txtDishWhen;
TextView txtActiGoal;
TextView txtActiActual;
EditText edtDishAtual;
public SelectDishActualAdapter(Context context, List<DishEntity> listDishInTime)
mContext = context;
Current = new String[listDishInTime.size()];
this.listDishInTime = listDishInTime;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
return listDishInTime.size();
@Override
public Object getItem(int position)
return listDishInTime.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView( int position, View convertView, ViewGroup parent)
View vi = convertView;
final int pos = position;
if (vi == null)
vi = inflater.inflate(R.layout.update_actual_dish_custom, null);
holder = new ViewHolder();
holder.lnlDishListView = (LinearLayout) vi.findViewById(R.id.lnlDishListView);
holder.lnlDishToGetpos = (LinearLayout) vi.findViewById(R.id.lnlDishToGetPos);
holder.txtTimeToEat = (TextView) vi.findViewById(R.id.txtEatTime);
holder.imgDish = (ImageView) vi.findViewById(R.id.imgDishActual);
holder.txtDishName = (TextView) vi.findViewById(R.id.txtDishActualName);
holder.txtActiGoal = (TextView) vi.findViewById(R.id.txtManualGram);
holder.txtActiActual = (TextView) vi.findViewById(R.id.txtActualGram);
holder.edtDishAtual = (EditText) vi.findViewById(R.id.edtActualGram);
holder.edtDishAtual.setId(pos);
vi.setTag(holder);
else
holder = (ViewHolder) vi.getTag();
///// id = 989898,989897,989896 is to decrale footer,It is mean Breakfast,Lunch and Dinner
if (listDishInTime.get(position).getDishId() == 989898)
holder.txtTimeToEat.setText(mContext.getResources().getString(R.string.sang));
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 40;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = 0;
else if (listDishInTime.get(position).getDishId() == 989897)
holder.txtTimeToEat.setText(mContext.getResources().getString(R.string.trua));
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 40;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = 0;
else if (listDishInTime.get(position).getDishId() == 989896)
holder.txtTimeToEat.setText(mContext.getResources().getString(R.string.chieu));
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 40;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = 0;
else
// Otherwise fill to listview with custom data
String temp = listDishInTime.get(position).getImageUrl();
final int imgID = mContext.getResources().getIdentifier(temp, "drawable", mContext.getPackageName());
holder.txtDishName.setText(listDishInTime.get(position).getName());
holder.imgDish.setImageResource(imgID);
int size = holder.imgDish.getLayoutParams().width;
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 0;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = size;
holder.edtDishAtual.addTextChangedListener(new TextWatcher()
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void afterTextChanged(Editable s)
holder.ref = pos;
Current[holder.ref] = s.toString(); // String[] Current with size = new size
myList.put(pos, s.toString().trim()); //mylist is hashmap
);
holder.edtDishAtual.setText(myList.get(pos));
return vi;
但是当我滚动时,editext 仍然会丢失内容数据 :(。请帮帮我
【问题讨论】:
阅读***.com/questions/11945563/…了解原因 你应该从写它的人那里了解视图回收:涡轮增压你的 UI:google.com/events/io/2009/sessions/… 是的,我知道问题所在,我将数据存储在 HashMap 中并再次调用它,但它仍然错误:( @kemdo 我认为您需要在更改对象DishEntity
后保存 editText 值
【参考方案1】:
ListView 回收它的子元素以优化内存的使用。当您的 EditText 项目移出屏幕时,它会被列表中的另一个项目使用。
这个来自 Udacity android 课程的short video 很好地解释了它是如何工作的。只需 1:40 分钟。
如果您希望您的 EditText 保留文本,您必须自己将其存储在项目数据中,然后在您的项目再次显示时恢复它。
【讨论】:
谢谢你,我通过将数据放入hashmap(数据=位置和文本)来存储数据,然后通过Edittext Settext(HashMap.getPosition)再次调用它,但它仍然错误:(以上是关于ListView为啥滚动listView时edittext数据丢失的主要内容,如果未能解决你的问题,请参考以下文章