我最近开始在 Android 上工作。我有一个列表视图,列表视图的行包含文本视图和复选框。我在我的活动程序中使用 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()){ ...请帮忙。非常感谢。