getView() method of ArrayAdapter repeats last item and copy to the first item

636 views Asked by At

I have a listview of EditText , they visualize three at a time and then scroll . the problem is that when I scroll down my first EditText fills the last , I can not understand what has gone wrong , can someone help me ?

public class EmailListAdapter extends ArrayAdapter<EmailContactBean> {

private Context mContext;
private int layoutResourceId;
private ListView listView;

private ArrayList<EmailContactBean> emailItem = new ArrayList<EmailContactBean>();



public EmailListAdapter(Context context, int layoutResourceId,
        ArrayList<EmailContactBean> arraylist) {
    super(context, layoutResourceId, arraylist);

    this.layoutResourceId = layoutResourceId;
    this.mContext = context;
    this.emailItem = arraylist;


}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.row_email_list, null);
    }



    final EmailContactBean item = emailItem.get(position);


    EditText editText = (EditText) convertView
            .findViewById(R.id.email_edit_text);

    editText.setText(item.getName());

    iconForType(item.getType(), editText);

    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable text) {



            String outputedText = text.toString();
            item.setName(outputedText);



        }
    });

    return convertView;
}

public void iconForType(int type, EditText edit) {

    switch (type) {
    case 0:

        edit.setCompoundDrawablesWithIntrinsicBounds(
                R.drawable.email_personal, 0, 0, 0);

        break;

    case 1:

        edit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.email_home,
                0, 0, 0);

        break;

    case 2:

        edit.setCompoundDrawablesWithIntrinsicBounds(
                R.drawable.email_works, 0, 0, 0);

        break;
    case 3:

        edit.setCompoundDrawablesWithIntrinsicBounds(
                R.drawable.email_other, 0, 0, 0);

        break;

    }

}

}

I solved it this way:

EmailListAdapter

public class EmailListAdapter extends ArrayAdapter<EmailContactBean> {

private Context mContext;
private int layoutResourceId;

private ArrayList<EmailContactBean> emailItem = new ArrayList<EmailContactBean>();

public EmailListAdapter(Context context, int layoutResourceId,
        ArrayList<EmailContactBean> arraylist) {
    super(context, layoutResourceId, arraylist);

    this.layoutResourceId = layoutResourceId;
    this.mContext = context;
    this.emailItem = arraylist;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    EmailContactBean ecb = new EmailContactBean();

    ecb = emailItem.get(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.row_email_list, null);

    }

    EditText editText = (EditText) convertView
            .findViewById(R.id.email_edit_text);


    editText.setTag(ecb);


    editText.addTextChangedListener(new GenericTextWatcher(convertView,
            R.id.email_edit_text));

    editText.setText(ecb.getName());

    iconForType(ecb.getType(), editText);


    return convertView;
}

public void iconForType(int type, EditText edit) {

    switch (type) {
    case 0:

        edit.setCompoundDrawablesWithIntrinsicBounds(
                R.drawable.email_personal, 0, 0, 0);

        break;

    case 1:

        edit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.email_home,
                0, 0, 0);

        break;

    case 2:

        edit.setCompoundDrawablesWithIntrinsicBounds(
                R.drawable.email_works, 0, 0, 0);

        break;
    case 3:

        edit.setCompoundDrawablesWithIntrinsicBounds(
                R.drawable.email_other, 0, 0, 0);

        break;

    }

}

}

GenericTextWatcher

public class GenericTextWatcher implements TextWatcher {

private View view;
private int editResource;


public GenericTextWatcher(View view, int editResource) {
    this.view = view;
    this.editResource = editResource;


}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {

    EditText edit = (EditText) view.findViewById(editResource);

    EmailContactBean ecb = (EmailContactBean) edit.getTag();

    String text = s.toString();

    ecb.setName(text);

}

}

0

There are 0 answers