滚动时ListView EditTexts值会搞乱

时间:2014-10-22 17:20:43

标签: android listview android-edittext

正如我在标题中提到的,listview在我的代码中的for循环中复制了i = 10之后的子项的edittexts值。

public class ExerciseAdapter extends ArrayAdapter<ExerciseSet> {
    private ArrayList<ExerciseSet> mExercise;
    private LayoutInflater inflater;

    private List<String> texts = new ArrayList<String>();

    public ExerciseAdapter(Context context, int textViewResourceId, ArrayList<ExerciseSet> objects) {
        super(context, textViewResourceId, objects);
        mExercise = objects;
        inflater = (LayoutInflater) getContext().
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {
        if(mExercise.get(position).isSet() == false) return 0;
        else return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 2; // two types of rows, exercise headers and bodies (sets)
    }


    public View getView(final int position, View convertView, ViewGroup parent){
        View view = convertView;
        final Holder holder;

        for(int i = 0; i < mExercise.size(); i++) {
            texts.add(String.valueOf(i));
        }

        if (getItemViewType(position) == 0) {

            if (view == null) {

                view = inflater.inflate(R.layout.item_exercise_header, null);
                holder = new Holder();


                holder.exerciseTitleView = (TextView) view.findViewById(R.id.tv_exercice_title);
                view.setTag(holder);

            } else {

                holder = (Holder) convertView.getTag();
            }
            ExerciseSet exercise = mExercise.get(position);

            if (exercise != null)
                holder.exerciseTitleView.setText(exercise.getExerciseTitle());


        } else {

            if (view == null) {
                view = inflater.inflate(R.layout.item_set, null);
                holder = new Holder();

                holder.setCounterView = (TextView) view.findViewById(R.id.tv_set_counter);

                holder.cbSet = (CheckBox) view.findViewById(R.id.cb_setdone);

                holder.repsCapt = (EditText) view.findViewById(R.id.et_reps_number);

                holder.repsCapt.setTag(position); 

                holder.repsCapt.setText(texts.get(position));

                //holder.weightCapt = (EditText) view.findViewById(R.id.et_weight_number);

                //db = new DatabaseHandler(this.getContext());

                //holder.weightCapt.setTag(position);
                view.setTag(holder);

            } else {

                holder = (Holder) convertView.getTag();

            }
            ExerciseSet exercise = mExercise.get(position);

            /*
             * edit texts
             */
            int tag_position=(Integer) holder.repsCapt.getTag();
            holder.repsCapt.setId(tag_position);

            holder.repsCapt.addTextChangedListener(new TextWatcher() {

                   @Override
                   public void onTextChanged(CharSequence s, int start, int before,
                           int count) {
                             final EditText repsText = (EditText) holder.repsCapt;

                             if(repsText.getText().toString().length()>0){
                                 texts.add(repsText.getText().toString());
                             }else{
                                 Toast.makeText(getContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
                             }

                         }

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

                   @Override
                   public void afterTextChanged(Editable s) {

                   }

               });

            if (exercise != null) {
                holder.setCounterView.setText(String.valueOf(exercise.getSetId()));
            }


            /*
             * check boxes 
             */

            holder.cbSet.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton button, boolean isChecked) {

                    if(isChecked)
                        mExercise.get(position).setCbChecked(true);
                    else
                        mExercise.get(position).setCbChecked(false);
                }
            });

            holder.cbSet.setChecked(exercise.isCbChecked());

        }

        return view;
    }

    static class Holder {
        public EditText weightCapt;
        public EditText repsCapt;

        // header row items
        TextView exerciseTitleView;

        // body row item
        CheckBox cbSet;
        TextView setCounterView;
    }

}

我不知道为什么,请帮忙..

Edit_1:我尝试将值保存到arraylist中并在重复使用视图时检索它们但是当我向下滚动时它仍然给出了重复的值。

Edit_2:如果我的问题不清楚,请告诉我们!

Edit_3:我将holder.repsCapt.setText(texts.get(position))放在if(exercise!= null){condition中,并且值不再重复。但是,如果我向下滚动,那么数值会变得混乱。

2 个答案:

答案 0 :(得分:2)

您的问题肯定与列表重复使用的项目视图有关 因此,请检查您的条件,并确保在所有情况下都提供替代文本。
开始,项目类型== 0,如下所示:

if (exercise != null)
                    holder.exerciseTitleView.setText(exercise.getExerciseTitle());

添加

else
    holder.exerciseTitleView.setText("debugging: nothing to show");


修改
同样是关于type == 1:
- holder.repsCapt.setText(texts.get(position))仅在新近膨胀的视图中被调用 - holder.setCounterView.setText(String.valueOf(exercise.getSetId()))仅当excersise不为空时

<强> EDIT2
好吧,对于 DEBUG 目的,请尝试不要在getView() - 始终从一开始就充分重用视图。

如果一切都变好(或者你得到一些空字段),那么回到我在这篇文章中的初步建议。我怀疑这种情况

如果问题仍然存在,则应注意根据位置正确获取数据。

答案 1 :(得分:0)

我在谷歌挖掘后设法解决了我的问题,结果如下:

@SuppressWarnings("unused")
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
    final Holder holder;
    convertView = null;
    if(getItemViewType(position) == 0) {
    if (convertView == null) {

        convertView = inflater.inflate(R.layout.item_exercise_header, null);
            holder = new Holder();
            holder.exerciseTitleView = (TextView)               convertView.findViewById(R.id.tv_exercice_title);
        convertView.setTag(holder);

        } else {

            holder = (Holder) convertView.getTag();
        }
        ExerciseSet exercise = exerciseList.get(position);

        if (exercise != null)
            holder.exerciseTitleView.setText(exercise.getExerciseTitle());

     }
     else if (getItemViewType(position) == 1) { 
     if (convertView == null) {
        holder = new Holder();

                convertView = inflater.inflate(R.layout.item_set, null);

                // reps e.t
                holder.repsCapt = (EditText) convertView
                        .findViewById(R.id.et_reps_number);

                holder.repsCapt.setTag(position);

                // weight e.t
                if(exerciseList.get(position).getExerSave()!=null) {
                    holder.repsCapt.setText(exerciseList.get(position).getExerSave().getReps());

                }
                // t.v
                holder.setCounterView = (TextView) convertView.findViewById(R.id.tv_set_counter);

                // c.b
                holder.cbSet = (CheckBox) convertView.findViewById(R.id.cb_setdone);

                convertView.setTag(holder);

            }else {
                holder = (Holder) convertView.getTag();
            }

            /*
             * text views
             */
            ExerciseSet exercise = exerciseList.get(position);

            if (exercise != null) {
                holder.setCounterView.setText(String.valueOf(exercise.getSetId()));
            }

            /* 
             * edit texts
             */
            int tag_position=(Integer) holder.repsCapt.getTag();

            holder.repsCapt.setId(tag_position);

            holder.repsCapt.addTextChangedListener(new TextWatcher() {

               @Override
               public void onTextChanged(CharSequence s, int start, int before,
                       int count) {
                   final int position2 = holder.repsCapt.getId();
                   final EditText repsText = (EditText) holder.repsCapt;
                   if(repsText.getText().toString().length()>0){
                       Save sv;

                       ExerciseSet es = exerciseList.get(position2);
                       if(es.getExerSave() != null)
                           sv = es.getExerSave();
                       else sv = new Save();

                       sv.setSet(position2);
                       sv.setReps(repsText.getText().toString());

                       es.setExerSave(sv);
                       exerciseList.set(position2,es);
                   }
               }

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

               }

               @Override
               public void afterTextChanged(Editable s) {

               }
            });

            /*
             * checkboxes
             */
            holder.cbSet.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton button, boolean isChecked) {

                    if(isChecked)
                        exerciseList.get(position).setCbChecked(true);
                    else
                        exerciseList.get(position).setCbChecked(false);
                }
            });

            holder.cbSet.setChecked(exercise.isCbChecked());    
        }
        return convertView;
    }