0

我有一个带有 [PICTURE] - [TEXT] - [CHECKBOX] 的自定义列表视图当我选择一个复选框并且我喜欢向下滚动时,会选择更多复选框。为什么呢?我该如何解决这个问题?

-> 我只选择一个复选框 -> 然后我向下滑动并选择了更多复选框

在此处输入图像描述 在此处输入图像描述 最好的问候 MSeiz5

我的自定义适配器_

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;
    }


}

日志猫:

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
4

6 回答 6

1

ListView 中的 CheckBox 的问题是视图由于 ListView 的回收而被回收,并且 Checkbox 的值(选中或取消选中)未被维护。为了保持 CheckBox 的状态,必须有一些东西可以存储 Checkbox 的状态。

请参阅此链接。它有很好的解释示例。

 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;
        }
于 2013-01-16T10:20:37.077 回答
0

我有同样的问题...

我做了什么:

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

这是一个例子:

            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;

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

于 2013-01-16T10:23:08.470 回答
0

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

于 2013-01-16T10:19:38.137 回答
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)));
  }
}
于 2013-01-16T10:43:46.450 回答
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

于 2013-01-16T10:13:55.923 回答
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);

        //...
     }

}
于 2013-01-16T10:45:11.693 回答