2012-08-03 30 views
0

我最近开始使用Android。我有一个列表视图,列表视图的行包含textview和复选框。我在我的活动程序中使用OnItemClickListener来获取选中的事件。我能够进入OnItemClickListenr。但在这里我无法得到检查的事件,我尝试了很多,但我的程序无法识别复选框选中的事件。请帮忙。下面是我的代码 - 列表视图的Android - Listview OnItemClickListener无法识别复选框选中事件

行 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/relativeLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="@android:color/white" > 

     <TextView 
      android:id="@+id/tvShapeDesc" 
      android:layout_width="230dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="25dp" 
      android:textColor="@android:color/black" /> 


    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="35dp" 
     android:background="@drawable/unchecked" 
     android:clickable="false" 
     android:focusable="false" 
     android:text="No" 
     android:visibility="visible"/> 

</RelativeLayout> 

下面是我的列表视图定义适配器的getview -

public View getView(int position, View convertView, ViewGroup parent) { 
     View itemView = null; 
     shapeObj = this.shapeList.get(position); 


      if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       itemView = inflater.inflate(R.layout.shapelist_item, null); 
      } else { 
     itemView = convertView; 
      } 

      TextView tvShape = (TextView) itemView.findViewById(R.id.tvShapeDesc); 
      final CheckBox cbSelection = (CheckBox) itemView.findViewById(R.id.checkBox1); 

      tvShape.setText(shapeObj.getName()); 

      cbSelection.setTag(position); 

     return itemView; 
    } 

下面是我OnItemClickListener -

lv.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) 
     { 

     Toast.makeText(getApplicationContext(), 
     "Click ListItem Number " + position, Toast.LENGTH_LONG).show(); 

     // CheckBox cbSelection = (CheckBox) view.getTag(position);  
     final CheckBox cbSelection = (CheckBox) view.findViewByI(R.id.checkBox1); 

      final Drawable checkedImage = view.getResources().getDrawable(R.drawable.checked); 
      final Drawable uncheckedImage = view.getResources().getDrawable(R.drawable.unchecked); 


     if((cbSelection).isChecked()){  
      Toast.makeText(getBaseContext(), "You checked " + position, Toast.LENGTH_SHORT).show(); 
      selectedCounter++; 
        cbSelection.setVisibility(CheckBox.INVISIBLE); 
        cbSelection.setBackgroundDrawable(checkedImage); 
        cbSelection.setVisibility(CheckBox.VISIBLE); 
     } else {      
      Toast.makeText(getBaseContext(), "You unchecked " + position, Toast.LENGTH_SHORT).show();  
      selectedCounter--; 
      cbSelection.setBackgroundDrawable(uncheckedImage); 
       cbSelection.setVisibility(CheckBox.VISIBLE); 
      } 

     shapeStr = Integer.toString(selectedCounter) + " shapes(s) selected."; 
     shapeCountStr.setText(shapeStr); 

     } 

程序((cbSelection).isChecked()){...请帮助。非常感谢。

+0

http://stackoverflow.com/questions/11737294/checkbox-and-setonitemclicklistener-not-working-in-android/11737567#11737567 – 2012-08-03 10:16:30

回答

0

试试下面的代码

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     {    

      if(isChecked) 
      { 
        // checkbox ckecked 
      } 
      else 
      { 
       // checkbox un-ckecked 
      } 
     } 
    });