Android:使用ViewHolder,OnCheckedChangedListener和TextChangedListener的自定义适配器

时间:2014-02-24 22:12:36

标签: java android android-listview custom-adapter

我是Android和Java编程的新手,最近我遇到了一个问题。几个小时我一直在寻找答案,但我没有成功。 我有一个自定义ListView和一个自定义适配器,其中一行包含CheckBox和EditText。每当它第一次显示时都没问题,但是当我向下滚动时,它会开始改变CheckBox和EditText的值。

这是我的row_edit.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/bonus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:inputType="numberSigned"
        android:gravity="right"
        android:selectAllOnFocus="true"
        android:textSize="16sp" />

    <CheckBox
        android:id="@+id/checkBox_person"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/bonus"
        android:textSize="16sp" />

</RelativeLayout>

Item_edit(适配器项目):

public class Item_edit {
    private String meno;
    private boolean ucast;
    private int bonus;

    public Item_edit(String meno, boolean ucast, int bonus) {
        super();
        this.meno = meno;
        this.ucast = ucast;
        this.bonus = bonus;
    }

    public String getMeno() {
        return meno;
    }

    public boolean getUcast() {
        return ucast;
    }

    public int getBonus() {
        return bonus;
    }

    public void setMeno(String meno) {
        this.meno = meno;
    }

    public void setUcast(boolean ucast) {
        this.ucast = ucast;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
}

当我滚动时ListView不顺畅,这就是我决定使用ViewHolder的原因。 Adapter_edit:

public class Adapter_edit extends ArrayAdapter<Item_edit> {
    private final Context context;
    private final ArrayList<Item_edit> itemsArrayList;
    private final Edit edit;

    public Adapter_edit(Context context, ArrayList<Item_edit> itemsArrayList, Edit edit) {

        super(context, R.layout.row_edit, itemsArrayList);
        this.edit = edit;
        this.context = context;
        this.itemsArrayList = itemsArrayList;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_edit, parent, false);
            holder = new ViewHolder();
            //Log.e("NewHolder", "position = " + position);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox_person);
            holder.bonus = (EditText) convertView.findViewById(R.id.bonus);
            convertView.setTag(holder);

            holder.checkBox.setText(itemsArrayList.get(position).getMeno());
            holder.checkBox.setChecked(itemsArrayList.get(position).getUcast());
            holder.bonus.setText(Integer.toString(itemsArrayList.get(position)
                    .getBonus()));

            holder.checkBox
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            itemsArrayList.get(position).setUcast(isChecked);
                        }
                    });

            holder.bonus.addTextChangedListener(new TextWatcher() {

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

                        if (itemsArrayList.get(position).getBonus() != Integer
                                .parseInt(s.toString()) && !edit
                                .getUcast(position)) {
                            itemsArrayList.get(position).setBonus(
                                    Integer.parseInt(s.toString()));
                            itemsArrayList.get(position).setUcast(true);
                        }
                    }
                    catch (NumberFormatException e) {
                        itemsArrayList.get(position).setBonus(0);
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {
                }

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

        }

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

        holder.checkBox.setText(itemsArrayList.get(position).getMeno());
        holder.checkBox.setChecked(itemsArrayList.get(position).getUcast());
        holder.bonus.setText(Integer.toString(itemsArrayList.get(position)
                .getBonus()));

        return convertView;
    }

    static class ViewHolder {
        CheckBox checkBox;
        EditText bonus;
    }
}

使用LogCat调试后,我知道只生成了8个持有者(因为有(0-7)个项目适合我的屏幕)。当我向下滚动时,这些正被重用。没有为位置大于7的项目创建OnCheckedChangeListeners和TextChangedListeners。

我不知道该怎么做。有人能帮助我吗?

1 个答案:

答案 0 :(得分:4)

我终于得到了答案。

我扩展了EditText,创建了我自己的类UpgEditText,它具有setOnTextChangedListener方法,这意味着删除现有的TextWatcher并添加一个新的TextWatcher。

UpgEditText

public class UpgEditText extends EditText {
    public UpgEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public UpgEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UpgEditText(Context context) {
        super(context);
    }

    TextWatcher watcher = null;

    public void setOnTextChangedListener(final int position, final Adapter_edit adapter) {
        if (watcher != null) {
            this.removeTextChangedListener(watcher);
        }

        watcher = 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) {
                try {
                    adapter.itemsArrayList.get(position).setBonus(
                            Integer.parseInt(s.toString()));
                }
                catch (Exception e) {
                }
            }
        };
        this.addTextChangedListener(watcher);
    }
}

我也经常使用ViewHolder:

public class ViewHolder {
    CheckBox chBoxUcast;
    UpgEditText edTBonus;
}

CustomAdapter getView()方法:

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_edit, parent, false);

            holder = new ViewHolder();
            holder.chBoxUcast = (CheckBox) convertView
                    .findViewById(R.id.checkBox_person);
            holder.edTBonus = (UpgEditText) convertView
                    .findViewById(R.id.bonus);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.edTBonus.setOnTextChangedListener(position, this);
        holder.chBoxUcast
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        itemsArrayList.get(position).setUcast(isChecked);
                    }
                });

        holder.chBoxUcast.setText(itemsArrayList.get(position).getMeno());
        holder.chBoxUcast.setChecked(itemsArrayList.get(position).getUcast());
        holder.edTBonus.setText(Integer.toString(itemsArrayList.get(position)
                .getBonus()));

        return convertView;

    }

现在一切正常。每次转换convertView时,我只会更改监听器并且运行良好。