2013-01-16 391 views
0

我有一個自定義列表視圖[PICTURE] - [TEXT] - [CHECKBOX] 當我選擇一個複選框並向下滾動時,選中的複選框更多。 這是爲什麼呢?我該如何解決這個問題?如何在CustomListview中正確使用CheckBox

- >我只是選擇一個複選框 - >然後我scoll下來,有選擇

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 

回答

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

+0

隨着CHOID_MODE_MULTIPLE我不能在我的列表視圖中選擇一個項目......當我選中的複選框問題依然存在...我爲我的問題添加截圖... –

0

嘗試設置您的複選框的標籤,並檢查代碼,同時設置複選框的值。

1

ListView內部的CheckBox問題是由於ListView的回收而導致視圖被回收,並且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; 
     } 
+0

我不明白它...它使我複雜 –

+0

他所說的是做我喜歡的事情 –

+0

@MarcoSeiz For你很容易理解,我添加了一個簡單的適配器,它有listitem作爲圖像,複選框,textview。檢查它:) – AndroidLearner

0

我有同樣的問題...

我所做的一切:

在我的對象,我把和boolean id_selected,在我CustomAdapter,我檢查如果選擇對象(當時我檢查checbox)或不在,並在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; 

我修改了我的一些類,但其實質在於......在我原來的類中,我使用反射和其他特定方法。

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

     //... 
    } 

} 
+0

什麼是EntAcc002? –

+0

@MarcoSeiz對不起。我編輯它。 – breceivemail

相關問題