在ListView中更改一个EditText会更改所有内容

时间:2016-12-16 08:17:21

标签: java android listview arraylist android-edittext

我知道有类似这样的问题,我已经按照这里发布的大量代码来达到这一点,但它们都没有适用于我的适配器。我也像许多其他人一样 使用ViewHolders因为我不太了解它们的用途。

在我的适配器类中,我想编辑editText以更改ArrayList事件对象中的相应值。但是,即使我使用EditText,现在更改某个事件mobileArrayList.get(position)字段也会对其进行更改。

public class CustomAdapter extends ArrayAdapter {

ArrayList<Event> mobileArrayList;


public CustomAdapter (Context context, ArrayList<Event> events){
    super(context, 0, events);
    this.mobileArrayList = events;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent){
    Event event = mobileArrayList.get(position);
    //ViewHolder holder;
    if(convertView == null){
        //holder = new ViewHolder();
       // holder.caption = (EditText) convertView.findViewById(R.id.events);
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_row, parent, false);
    } else {
        //holder = (ViewHolder) convertView.getTag();
    }


    EditText events = (EditText) convertView.findViewById(R.id.events);
    EditText start = (EditText) convertView.findViewById(R.id.start);
    EditText end = (EditText) convertView.findViewById(R.id.end);
    TextView day = (TextView) convertView.findViewById(R.id.day);

    events.setText(event.getSummary());
    start.setText("Start Date: " +event.getDateStart());
    end.setText("End Date: " + event.getDateEnd());
    day.setText(event.getDayOfTheWeek());

    /*holder.caption.setId(position);

    holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                final int position = v.getId();
                final EditText events = (EditText) v;
                mobileArrayList.get(position).setSummary(events.getText().toString());

            }
        }
    });*/

    events.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String edit = s.toString();
            mobileArrayList.get(position).setSummary(edit);
        }
    });

    return convertView;
}

class ViewHolder{
    EditText caption;
}

}

我还应该注意,一旦我为事件EditText字段工作,我也会对其他两个字段做同样的事情。谢谢

编辑:如果我尝试将其与ViewHolders一起使用,它会在我尝试分配第一个holder.events = (EditText) convertView.findViewById(R.id.events)时抛出一个NPE。以下是使用ViewHolders的代码:

public class CustomAdapter extends ArrayAdapter {

ArrayList<Event> mobileArrayList;


public CustomAdapter (Context context, ArrayList<Event> events){
    super(context, 0, events);
    this.mobileArrayList = events;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent){
    Event event = mobileArrayList.get(position);
    ViewHolder holder;
    if(convertView == null){
        holder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_row, parent, false);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    /*EditText events = (EditText) convertView.findViewById(R.id.events);
    EditText start = (EditText) convertView.findViewById(R.id.start);
    EditText end = (EditText) convertView.findViewById(R.id.end);
    TextView day = (TextView) convertView.findViewById(R.id.day);
    */

    holder.events = (EditText) convertView.findViewById(R.id.events);
    holder.start = (EditText) convertView.findViewById(R.id.start);
    holder.end = (EditText) convertView.findViewById(R.id.end);
    holder.day = (TextView) convertView.findViewById(R.id.day);

    holder.events.setText(event.getSummary());
    holder.start.setText("Start Date: " +event.getDateStart());
    holder.end.setText("End Date: " + event.getDateEnd());
    holder.day.setText(event.getDayOfTheWeek());

    /*holder.caption.setId(position);

    holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                final int position = v.getId();
                final EditText events = (EditText) v;
                mobileArrayList.get(position).setSummary(events.getText().toString());

            }
        }
    });*/

    holder.events.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String edit = s.toString();
            mobileArrayList.get(position).setSummary(edit);
        }
    });

    return convertView;
}

class ViewHolder{
    EditText events;
    EditText start;
    EditText end;
    TextView day;
}

}

1 个答案:

答案 0 :(得分:6)

尝试删除if(convertView == null)条件。