I am trying to check the checkbox of my custom listView items.
I am trying by using view.setOnClickListener on my custom listview Adapter.
When I am selecting one item of the listView item, another item is also getting selected down of the list.
My getView code
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (view == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(row, parent, false);
holder = new ViewHolder();
holder.studentName = (TextView) view.findViewById(R.id.studentName);
holder.id = (TextView) view.findViewById(R.id.studentID);
holder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
view.setTag(holder.checkBox);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!holder.checkBox.isChecked() && view!=null) {
holder.checkBox.setChecked(true);
presentStudent.add(data.get(position));
Toast.makeText(context, "added " + data.get(position).getName(), Toast.LENGTH_SHORT).show();
}
else {
holder.checkBox.setChecked(false);
presentStudent.remove(data.get(position));
Toast.makeText(context, "removed " + data.get(position).getName(), Toast.LENGTH_SHORT).show();
}
}
});
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if((data == null) || ((position+1) > data.size()))
return view;
studentData = data.get(position);
if ((holder.studentName != null) && null != studentData.getName()
&& studentData.getName().trim().length() > 0 )
holder.studentName.setText(Html.fromHtml(studentData.getName()));
if ((holder.id != null) && null != studentData.getAcademicId()
&& studentData.getAcademicId().trim().length() > 0 )
holder.id.setText(Html.fromHtml(studentData.getAcademicId()));
return view;
}
Here is my Layout code:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="10dp"
android:text="ID: 20151057010 (057)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/studentID"
android:layout_alignBottom="@+id/checkBox"
android:layout_alignLeft="@+id/studentName"
android:layout_alignStart="@+id/studentName" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/image"
android:src="@drawable/student_icon"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:layout_below="@+id/studentName"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp" />
<TextView
android:id="@+id/studentName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sudarshan Mondal"
android:textSize="20sp"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_alignTop="@+id/image"
android:layout_toRightOf="@+id/image"
android:layout_toEndOf="@+id/image"
android:layout_marginTop="10dp"/>
</RelativeLayout>
</LinearLayout>
What should I do?

