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()){...請幫助。非常感謝。
http://stackoverflow.com/questions/11737294/checkbox-and-setonitemclicklistener-not-working-in-android/11737567#11737567 – 2012-08-03 10:16:30