如何正确使用自定义列表视图中的CheckBox

时间:2013-01-16 10:10:06

标签: android checkbox android-listview

我有自定义列表视图[图片] - [文字] - [复选框] 当我选择一个复选框并且我想向下滚动时,会选中更多复选框。 为什么会这样?我该如何解决这个问题呢?

- >我只选择一个复选框 - >然后我瘫痪了,还有更多的复选框被选中

enter image description here enter image description here 最好的祝福 MSeiz5

我的CustomAdapter _

public class CustomAdapter extends BaseAdapter{
            TextView rawTextView;

            boolean[] checkBoxState;
            LayoutInflater mInflater;   
            ListContent holder;
            private ArrayList<Notification> arrNotification=null;

    private ArrayList<HashMap<String, String>> arrlist = new ArrayList<HashMap<String, String>>();


    public CustomAdapter(ArrayList<HashMap<String, String>> arrlist) {

        arrNotification = new ArrayList<Notification>();

        mInflater = LayoutInflater.from(connected_upload.this);
        checkBoxState=new boolean[arrNotification.size()];            
        this.arrlist = arrlist;
    }


    @Override
    public int getCount() {
        return arrlist.size();
    }

    public int getCount2()
    {
        return arrNotification.size();      
    }

    public Object getItem2(int position) 
    {
        return position;
    }
    public long getItemId2(int position) 
    {
        return position;
    }

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

        convertView = mInflater.inflate(R.layout.connected_upload, null);
        holder=new ListContent();
        holder.checkbox = (CheckBox) convertView.findViewById( R.id.checkBox1 );


        convertView.setTag(holder);

        if(convertView == null){
            // LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.connected_upload_row, null);

            holder=new ListContent();
            holder.checkbox = (CheckBox) convertView.findViewById( R.id.checkBox1 );

            convertView.setTag(holder);
        }
                   // R.id.img_list, R.id.string_cell
                     maptemp=new HashMap<String, String>();
                     maptemp=arrlist.get(position);
        rawTextView = (TextView) convertView.findViewById(R.id.string_cell);
        rawTextView.setText(maptemp.get("string_cell"));


            if (isfolder == true){
                imgview=(ImageView) convertView.findViewById(R.id.img_list);
                imgview.setImageResource(R.drawable.folder);

            } else {


                    Bitmap test = getbitpam(maptemp.get("img_list"));
                    imgview=(ImageView) convertView.findViewById(R.id.img_list);

                    imgview.setImageBitmap(test); 

                    System.gc();

            }
        return convertView;
    }


    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }


}

logcat的:

01-16 13:41:05.885: E/AndroidRuntime(1720): FATAL EXCEPTION: main
01-16 13:41:05.885: E/AndroidRuntime(1720): java.lang.NullPointerException
01-16 13:41:05.885: E/AndroidRuntime(1720):     at mseiz.freeftp.connected_upload$CustomAdapter.getView(connected_upload.java:306)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.AbsListView.obtainView(AbsListView.java:2465)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.ListView.onMeasure(ListView.java:1162)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2361)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5057)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.View.measure(View.java:15481)
01-16 13:41:05.885: E/AndroidRuntime(1720):     at android.view.ViewGroup.measureChildWithMargins(V

6 个答案:

答案 0 :(得分:1)

CheckView内部CheckView的问题在于,由于ListView的回收而导致视图被回收,并且未维护Checkbox(检查或取消选中)的值。为了将状态保持为CheckBox,必须存在可以存储Checkbox状态的东西。

请参阅此Link。它已经很好地解释了示例。

 public class ListViewAdapter extends BaseAdapter
 {
            private boolean[] checkBoxState;
            private LayoutInflater mInflater;   
            private ListContent holder;
            private ArrayList<Notification> arrNotification=null;
            public ListViewAdapter(Context context,ArrayList<Notification> notify)
            {     
                arrNotification = new ArrayList<Notification>();

                mInflater = LayoutInflater.from(context);
                checkBoxState=new boolean[arrNotification.size()];
                imageLoader=new ImageLoader(NotificationListActivity.this.getApplicationContext());
            }
            public int getCount()
            {
                return arrNotification.size();      
            }

            public Object getItem(int position) 
            {
                return position;
            }
            public long getItemId(int position) 
            {
                return position;
            }
            public View getView(final int position, View convertView, ViewGroup parent)
            {
                if (convertView == null) 
                {
                    convertView = mInflater.inflate(R.layout.noti_list, null);
                    holder=new ListContent();
                    holder.entity_type = (TextView) convertView.findViewById(R.id.entityname_textview);
                    holder.noti_thumb = (ImageView) convertView.findViewById(R.id.img_noti_thumb);
                    holder.checkbox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );

                    convertView.setTag(holder);
                } 
                else
                {
                    holder = (ListContent)convertView.getTag();
                }           

                holder.noti_thumb.setTag(arrNotification.get(position).thumbnail);
                imageLoader.DisplayImage(arrNotification.get(position).thumbnail,NotificationListActivity.this, holder.noti_thumb);

                holder.checkbox.setChecked(checkBoxState[position]);

                holder.checkbox.setOnClickListener(new OnClickListener()
                {
                    public void onClick(View v) 
                    {
                        if (((CheckBox) v).isChecked())
                        {
                            checkBoxState[position]=true;

                        }
                        else
                        {
                            checkBoxState[position]=false;


                        }
                    }
                });


                holder.checkbox.setTag(position);

                return convertView; 
            }
        }

        class ListContent 
        {
            TextView entity_type;
            TextView content_type;
            ImageView noti_thumb;
            CheckBox checkbox;
        }

答案 1 :(得分:0)

试试这个......它可能对你有帮助。

mList.setAdapter(new ArrayAdapter<String>
(mSplitActivity,android.R.layout.simple_list_item_multiple_choice, mStrings));
        mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

也试试这个 http://androidcocktail.blogspot.in/2012/04/adding-checkboxes-to-custom-listview-in.html

答案 2 :(得分:0)

尝试设置复选框的标记,并在设置复选框的值时检查标记。

答案 3 :(得分:0)

我遇到了同样的问题...

我做了什么:

在我的对象中,我放了boolean id_selected并在我的CustomAdapter中检查对象是否被选中(然后我检查了checbox),并在getView中禁用了可点击的复选框,在convertview点击中,我将id_selected反转并刷新CheckBox。有了这个,当用户点击复选框或在行中时,他们选中或取消选中复选框,可以做你想要的......

以下是一个例子:

            final T t = getItem(position);

            if (convertView == null) {
                convertView = inflater.inflate(resourceId, parent, false);
            }

            final View vv = convertView;

            convertView.setClickable(true);
            convertView.setFocusable(true);
            convertView.setBackgroundResource(R.drawable.listview_background);

                  TextView tv_ds_lista          = (TextView) convertView.findViewById(R.id.ds_lista);
            final CheckBox chkbx_id_selecionado = (CheckBox) convertView.findViewById(R.id.id_selecionado);

            chkbx_id_selecionado.setClickable(false);

            convertView.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {    
                        chkbx_id_selecionado.setChecked(!chkbx_id_selecionado.isChecked());

                    t.setId_selecionado(chkbx_id_selecionado.isChecked());

                }
            });

            tv_ds_lista.setText(t.toString());

            return convertView;

我修改了我的一些课程,但本质就在那里......在我的原始课程中,我使用了反射和其他特定的方法。

答案 4 :(得分:0)

List<String> recIdArr = new ArrayList<String>();
List<String> selectedIds = new ArrayList<String>();
List<String> reportslist;
public View getView(final int position, final View convertView, ViewGroup parent) 
{
  View row = super.getView(position, convertView, parent);
  CheckBox cBox=(CheckBox)row.findViewById(R.id.cb1);

  if(cBox.isChecked())
  {
    selectedIds.add(recIdArr.get(reportslistview.getPositionForView(cBox)));    
  }
  else
  {
    selectedIds.remove(recIdArr.get(reportslistview.getPositionForView(cBox)));
  }
}

答案 5 :(得分:0)

您必须使用自定义适配器存储项目状态,如下例所示:

public class MyAdapter extends ArrayAdapter<MyClass> {
// MyClass has a field named 'selected'

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

        View rowView;
        final MyClass item = getItem(position);

        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflator.inflate(rsc, null);
        } else {
            rowView = (View) convertView;
        }

       //...
       CheckBox chkSelected = (CheckBox) rowView
                .findViewById(R.id.chkSelected);
       chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                item.selected = isChecked;
            }
        });
        chkSelected.setChecked(item.selected);

        //...
     }

}