ArrayAdapter的getView()方法重复最后一项并复制到第一项

时间:2014-12-01 16:45:21

标签: android listview arraylist android-arrayadapter textwatcher

我有一个EditText的列表视图,它们一次可视化三个然后滚动。问题是,当我向下滚动我的第一个EditText填充最后一个时,我无法理解出了什么问题,有人可以帮助我吗?

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;

    }

}

}

我这样解决了:

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 个答案:

没有答案