<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<CheckBox
android:id="@+id/book_check"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:checked="false" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/book_check"
android:orientation="horizontal"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/finger" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/book_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Title"
android:textColor="#000"
android:textSize="22sp" />
<TextView
android:text="Description"
android:id="@+id/book_descri"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF2500"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
在我的自定义类中,我有以下代码来帮助我管理从小数据库中获取的数据
private class SbListAdapter extends ArrayAdapter<SbItem> {
ArrayList<SbItem> SongBookList;
public SbListAdapter(Context context, int textViewResourceId,
ArrayList<SbItem> SongBookList) {
super(context, textViewResourceId, SongBookList);
this.SongBookList = new ArrayList<SbItem>();
this.SongBookList.addAll(SongBookList);
}
private class ViewHolder {
CheckBox book;
TextView title;
TextView descri;
}
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null; Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.cc_list_select, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.book_title);
holder.descri = (TextView) convertView.findViewById(R.id.book_descri);
holder.book = (CheckBox) convertView.findViewById(R.id.book_check);
convertView.setTag(holder);
holder.book.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
SbItem SbItem = (SbItem) cb.getTag();
SbItem.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
SbItem SbItem = SongBookList.get(position);
holder.title.setText(SbItem.getBook());
holder.descri.setText(SbItem.getDescri());
holder.book.setChecked(SbItem.isSelected());
holder.book.setTag(SbItem);
return convertView;
}
}
我的愿望是,当单击第一行和第一行中的文本时,选中或取消选中该复选框,因为目前这仅在单击复选框时才有效。 如果我尝试输入强制转换,则只会出错。例如,这不起作用:
holder.title.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
SbItem SbItem = (SbItem) cb.getTag();
SbItem.setSelected(cb.isChecked());
}
});
当用户点击该项目的任何地方时,我只需要工作,就像在设置活动中一样。
我试过这样做:
convertView.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
SbItem SbItem = (SbItem) cb.getTag();
SbItem.setSelected(cb.isChecked());
}
});
但在调试崩溃时,控制台上的错误是:
08-31 13:25:19.793:E / AndroidRuntime(24214): java.lang.ClassCastException:android.widget.RelativeLayout不能 强制转换为android.widget.CheckBox
答案 0 :(得分:1)
试试这个:
从您的活动或您创建列表视图的任何位置:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//here update the status of your SbItem in the position of your arraylist and then notify the adapter
sblist.get(position).setSelected(!sblist.get(position).isSelected());
adapter.notifyDataSetChanged();
}
});
同时从适配器中删除所有点击侦听器。
<强>更新强>
在你的SbItem类中添加setter:
public void setSelected(boolean s){
this.selected=s;
}
此外,我假设你的getter返回selected
属性
public boolean isSelected(){
return selected;
}
答案 1 :(得分:0)
使用以下事件更改CheckBox状态
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkbox.setChecked(true);
} else {
checkbox.setChecked(false);
}
}
});
答案 2 :(得分:-1)
作为您的问题,您需要更改代码。
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.book.performClick();
}
});
现在使用setOnCheckedChangeListener
复选框。
holder.book.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.setChecked(true);
// do your stuff
} else {
buttonView.setChecked(false);
// do your stuff
}
}
});