1

I follow some code in few page, but it not working. Here is my code of 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;
}

But when I scroll, editext still lose content data :(. please help me

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
kemdo
  • 1,429
  • 3
  • 15
  • 29

1 Answers1

0

ListView recycle it's children to optimise use of the memory. When your EditText item is moved out of the screen it gets used by another item from your list.

This short video from Udacity Android course explains nicely how it works. It takes just 1:40min.

If you want your EditText to remain the text you have to store it yourself within item data and then restore it when your item is shown again.

Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • thanks you, I were storing data by put data in hashmap ( data = position and text), then call it again by Edittext Settext (HashMap.getPosition), But it still wrong :( – kemdo Aug 13 '14 at 07:52