2013-03-12 66 views
0

嗨,我創建一個自定義列表視圖與動態複選框添加,我得到我想要的,但是當我嘗試在第一行中選擇一個複選框,然後自動第一個按鈕在第5,9,13行被選中,我選擇第二行中的任何按鈕,然後在6日,8日同一個按鈕,第12行是越來越選擇什麼,我做錯了,我的適配器類列表視圖複選框與自定義列表視圖中使用android中的RadioButton?

class listViewEventscustomAdapter extends BaseAdapter { 
    LayoutInflater mInflater; 
    ViewHolder holder; 

    public listViewEventscustomAdapter(Context context) { 
     mInflater = LayoutInflater.from(context); 

    } 

    public int getCount() { 
     if (arrListViewoption_title != null) { 
      return arrListViewoption_title.size(); 
     } else { 
      Toast.makeText(getApplicationContext(), "No item found", 
        Toast.LENGTH_LONG).show(); 
      return 0; 
     } 
    } 

    public Object getItem(int arg0) { 
     return arg0; 
    } 

    public long getItemId(int arg0) { 
     return arg0; 
    } 

    @SuppressWarnings("unchecked") 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 
     View v = convertView; 
     final CheckBox checkitem; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.customlistviewdata, null); 
     } 
     if (v != null) { 
      holder = new ViewHolder(); 
      holder.txtoptiontitle = (TextView) v 
        .findViewById(R.id.optiontitle); 
      holder.txtsubtitle = (TextView) v.findViewById(R.id.subtitle); 
      checkitem = (CheckBox) v.findViewById(R.id.checkBox1); 
      holder.btn_radio = (RadioButton) v 
        .findViewById(R.id.radio0); 

      holder.txtoptiontitle.setText(arrListViewoption_title 
        .get(position)); 
      holder.txtsubtitle.setText(arrlistViewsub_title.get(position)); 

      if (value.equals(arrListViewoption_type.get(position) 
        .toString().trim())) { 
       checkitem.setVisibility(View.VISIBLE); 
       holder.btn_radio.setVisibility(View.GONE); 

      } 

      else { 
       holder.btn_radio.setVisibility(View.VISIBLE); 
       checkitem.setVisibility(View.GONE); 

      } 

      checkitem 
        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

         @Override 
         public void onCheckedChanged(CompoundButton view, 
           boolean isChecked) { 
          // int getPosition = (Integer) view.getTag(); 
          // list.get(getPosition).setSelected(view.isChecked()); 

          if (view.isChecked()) { 

           checkboxvalue.add(arrlistViewsub_title 
             .get(position).toString().trim()); 
          } else { 
           System.out 
             .println("*************unchecked"); 
           checkboxvalue.remove(arrlistViewsub_title 
             .get(position).toString().trim()); 
           adapter_list.notifyDataSetChanged(); 

          } 

          Log.i(this.toString(), 
            "Item" + checkboxvalue.size()); 

         } 
        }); 

      holder.btn_radio 
        .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

         @Override 
         public void onCheckedChanged(
           CompoundButton buttonView, boolean isChecked) { 
          // TODO Auto-generated method stub 
          if (buttonView.isChecked()) { 


           // Action Doing here 

          } 
         } 
        }); 

      v.setTag(holder); 

     } 
     return v; 

    } 

} 
+0

給我任何解決方案 – Gomathi 2013-03-12 12:34:20

+0

http://stackoverflow.com/a/14154397/646806這也太http://robinhood87.blogspot.in/ – RobinHood 2013-03-12 12:41:59

回答

1

使用convertview是有用的performancce和快速滾動,但可能會導致到那種問題。

重新使用之前,您應該重置轉換視圖中的任何設置值。在你的情況下:

boolean wasItemChecked = checkboxvalue.contains(arrlistViewsub_title.get(position).toString().trim()); 
checkItem.setChecked(wasItemChecked); 

如果checkItem狀態已更改,它會根據數據源狀態將其重置爲其真實狀態。

希望有幫助。

+0

如何檢查是否條件? – Gomathi 2013-03-13 06:26:02

相關問題