Android - Listview OnItemClickListener无法识别复选框选中的事件

时间:2012-08-03 10:00:46

标签: android android-listview

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

列表视图行 -

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

以下是我的listview自定义适配器的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);

        }

程序不在里面 - if((cbSelection).isChecked()){...请帮忙。非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试以下代码

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

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