问题
I wish to populate a listview which contains checkbox as list items in an android application. I have implemented a listview, but if I check anyone of the checkboxes in the list it checks some other lists in the listview.. Thanks in advance..
For custom Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/chk_check"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:singleLine="true"
android:text="@string/check_text"/>
</LinearLayout>
For the listview:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
For Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ListHolder holder;
row = convertView;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ListHolder();
holder.chk = (CheckBox) row
.findViewById(R.id.chk_check);
row.setTag(holder);
} else {
holder = (ListHolder) row.getTag();
}
if (list != null) {
String data = list.get(position);
holder.chk.setText(data);
}
return row;
}
public class ListHolder {
CheckBox chk;
}
All are working fine, but the checkboxes checked itself when I scroll the listview.
来源:https://stackoverflow.com/questions/19980376/checkbox-within-a-listview