vendredi 20 janvier 2017

Select Check Boxes in ListView[( Select multiple checkbox but one by one), Select All at once, Select only one at a time]

I have to Select Check boxes in ListView. Right now when I am selecting the checkbox, some check boxes below got checked automatically. This is my code:

StudentAdapter.java:

public class StudentAdapter extends BaseAdapter{
private Activity activity;
private List<StudentBean> studentBeanList;
private boolean checked = false;
CheckBox checkBox;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
    super();
    this.studentBeanList = studentBeanList;
    this.activity = activity;
}
@Override
public int getCount() {
    return studentBeanList.size();
}
@Override
public Object getItem(int position) {
    return studentBeanList.get(position);
}
@Override
public long getItemId(int position) {
    return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    StudentAdapter.ItemHolder itemHolder= new StudentAdapter.ItemHolder();
    if (convertView==null) {
        LayoutInflater li = (LayoutInflater)
                (activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));

        convertView = li.inflate(
                R.layout.row_student, null);
       itemHolder.textViewStudent = (TextView) convertView
                .findViewById(R.id.tvstudentname);
        convertView.setTag(itemHolder);
        itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        convertView.setTag(itemHolder);
    }
    else
    {
        itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();

    }
    if (studentBeanList != null && !studentBeanList.isEmpty())
    {
        final StudentBean studentBean = studentBeanList.get(position);
        if (studentBean != null) {
            if
                    (itemHolder.textViewStudent != null && studentBean.getStudentname() != null)
            {
               itemHolder.textViewStudent.setText(studentBean.getStudentname());
            }
        }
    }
    return convertView;
}
private class ItemHolder {
    TextView textViewStudent;
    CheckBox checkBox;
}
@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    // Your code to nofify
}
}

I am inflating below XML..

row_student.xml

 <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/tvstudentname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:padding="10dp"
    android:layout_alignBaseline="@+id/checkBox1"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/checkBox1"
    android:layout_toStartOf="@+id/checkBox1" />
<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
 </RelativeLayout>

I am helpless what to do. How can I get Select All, Select only one (I mean when user select one, other dis selects auto), Select All but one by one. Thanks in Advance..




Aucun commentaire:

Enregistrer un commentaire