我的自定义列表视图中的复选框的 setOnClickListener 永远不会被调用。下面是我的代码 - 任何指针都会有所帮助。
调用 setOnCheckedChangeListener ,但 setOnClickListener 不会被调用。
以下是我的自定义ListAdapter:
public class Listadapter extends BaseAdapter {
private static String TAG = "focus";
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
private final boolean[] itemChecked;
List<String> appNamestoBlock_local;
public Listadapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager, List<String> appNamestoBlock) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
this.appNamestoBlock_local = appNamestoBlock;
itemChecked = new boolean[packageList.size()];
}
private String getSerializedBlockedAppNames() {
String serializedBlockedAppNames;
Log.v(TAG,
"-------- List<String> list = "
+ TextUtils.join(",", appNamestoBlock_local));
serializedBlockedAppNames = TextUtils.join(",", appNamestoBlock_local);
return serializedBlockedAppNames;
}
private class ViewHolder {
TextView apkName;
CheckBox ck1;
}
public int getCount() {
return packageList.size();
}
public Object getItem(int position) {
return packageList.get(position);
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Log.v(TAG + " Listadapter", "entered getView(...)");
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView
.findViewById(R.id.textView1);
holder.ck1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
//debug
holder.ck1.setFocusable(false);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 40, 40);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
// holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else {
holder.ck1.setChecked(false);
Log.v(TAG, "setting Checkbox=false, in ListAdapter.java, line 108");
}
// Log.v(TAG,
// "-------- checking in getView() in ListAdapter if appName is in getSerializedBlockedAppNames()");
if (getSerializedBlockedAppNames().contains(appName)) {
Log.v(TAG + " ListAdapter",
"-------- YES, appName is in getSerializedBlockedAppNames(), appName = "
+ appName);
holder.ck1.setChecked(true);
}
holder.ck1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Log.v(TAG + " Listadapter",
"isChecked=TRUE in Listadapter.java, item[position] = "
+ String.valueOf(itemChecked[position]));
} else {
Log.v(TAG + " Listadapter",
"isChecked=FALSE in Listadapter.java, item[position] = "
+ String.valueOf(itemChecked[position]));
}
}
});
holder.ck1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v(TAG + " Listadapter",
"Entered holder.ck1.setOnClickListener ");
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
Log.v(TAG + " Listadapter",
"in ck1.onClick and setting itemChecked = TRUE");
} else {
Log.v(TAG + " Listadapter",
"in ck1.onClick and setting itemChecked = FALSE");
itemChecked[position] = false;
}
}
});
return convertView;
}
}
答案 0 :(得分:0)
试试这个。
holder.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}