我在几页中关注了一些代码,但它无法正常工作。这是我的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仍会丢失内容数据:(。请帮助我
答案 0 :(得分:0)
ListView回收它的孩子以优化内存的使用。当您的EditText项目移出屏幕时,它会被列表中的其他项目使用。
来自Udacity Android课程的short video很好地解释了它是如何工作的。它只需要1:40分钟。
如果您希望EditText保留文本,您必须自己将其存储在项目数据中,然后在再次显示项目时将其恢复。