1

我遇到的问题是当我单击一个复选框并使列表视图向上滚动选定的复选框时,或者有时如果选择第一个复选框,那么最后一个复选框也被选中。我知道我的英语还不够好,很抱歉。任何帮助表示赞赏。

import java.util.ArrayList;
import java.util.Hashtable;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

public class MyListAdapter extends BaseAdapter{

     public static class ViewHolder {

         public CheckBox chkb = null;

    } 


    private LayoutInflater mInflater;

    private ArrayList<MyListAdapterItem> myListAdapterItems = null;

    private Hashtable<Object, Object> items = null;

    private boolean selectAll = false;

    private boolean readOnly = false;

    public MyListAdapter(Context context, ArrayList<MyListAdapterItem> myListAdapterItems, boolean selectAll, boolean readOnly) {

        mInflater = LayoutInflater.from(context);

        this.myListAdapterItems = myListAdapterItems;

        this.selectAll = selectAll;

        this.readOnly = readOnly;

        items = new Hashtable<Object, Object>();
    }

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

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.chkb_fila, null);

            viewHolder = new ViewHolder();

            viewHolder.chkb = (CheckBox) convertView.findViewById(R.id.chkbCultivo);

            convertView.setTag(viewHolder);

        }else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        items.put(myListAdapterItems.get(position), viewHolder.chkb);

        viewHolder.chkb.setText(myListAdapterItems.get(position).getDescription());

        if(selectAll){

            viewHolder.chkb.setChecked(true);
        }

        if(readOnly){

            viewHolder.chkb.setEnabled(false);

        }

        return convertView;

    }


    public int getCount() {

        return myListAdapterItems.size();

    }


    public Object getItem(int position) {

        return myListAdapterItems.get(position);

    }


    public long getItemId(int position) {

        return position;

    }

    /**
     * @return the myListItems
     */
    public ArrayList<MyListAdapterItem> getMySelectedListItems() {

        ArrayList<MyListAdapterItem> listSelectedItems = new ArrayList<MyListAdapterItem>();

        CheckBox tmpCheckBox = null;

        for(int i=0; i<items.size(); i++){

            tmpCheckBox = (CheckBox) items.get(myListAdapterItems.get(i));

            if(tmpCheckBox.isChecked()){

                listSelectedItems.add(myListAdapterItems.get(i));

            }

        }

        return listSelectedItems;

    }

}
4

3 回答 3

0

问题是该getView方法重用了视图,因此您必须确保更新该方法内的复选框状态;这也意味着您必须将该状态保存在某处。

于 2011-11-02T15:52:45.873 回答
0

以下是您正在寻找的一些链接。高级,但如果你坚持下去并且不放弃这一点,你会学到很多!看一看:

Android:具有多个可点击按钮的 ListView 元素

带有嵌套小部件的 Android 自定义列表项

listItems 上的评分栏和按钮

于 2011-11-02T15:56:40.673 回答
0

2个选项。

1)不要回收你的观点。总是给一个新的充气。那是“懒惰”,但如果您的应用程序没有太多条目,则不太可能引入错误。

2)确保在每个视图上设置所有内容。您正在重用它们,因此如果您之前在其中设置了一些东西,它将保持这种状态。

改变这个:

if(selectAll){

        viewHolder.chkb.setChecked(true);
    }

    if(readOnly){

        viewHolder.chkb.setEnabled(false);

    }

        viewHolder.chkb.setChecked(selectAll);
        viewHolder.chkb.setEnabled(!readOnly);

使用 if 块,您不会设置布尔值的另一侧(您也可以使用 else 块,但这更冗长)。

于 2011-11-02T16:05:24.643 回答