0

I am binding Checkbox using Custom ListView. Now, at the last, I want all the values of checked CheckBox.

How to get this ? I want to know that how can I get values using my code. ListView itemOnClickListener is not firing here.

My code :

Map<String, String> m = null;
for (int i = 0; i < lList.size(); i++) {
    objSubCatData = lList.get(i);

    lstSubCatId.add(objSubCatData.getSubCatId());

    m = new HashMap<String, String>();
    m.put("SubCatName", objSubCatData.getSubCatName());

    aaSubCategory.add(m);
}

final SimpleAdapter adapter = new SimpleAdapter(UserInterest.this, aaSubCategory, R.layout.userinterest_1, new String[] { "SubCatName" }, new int[] { R.id.chkUserInterest });

    lvUserInterest.setAdapter(adapter);

layout1.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/datalist"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/chkUserInterest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/blue"
        android:textSize="14sp" >
    </CheckBox>

</LinearLayout>

Layout2.xml :

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/lvUserInterest"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginTop="8dp"
                android:divider="#b5b5b5"
                android:dividerHeight="1dp" />

            <Button
                android:id="@+id/btnUTSubmit"
                android:layout_width="130dp"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:layout_marginTop="6dp"
                android:background="@drawable/blue_bg"
                android:text="@string/Submit"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="@string/userinterest"
                android:textColor="@color/gray" />
        </LinearLayout>
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
  • http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button – Raghunandan Apr 07 '14 at 11:22
  • possible duplicate of [Get Checked values of CheckBox which is bind using Custome Listview android](http://stackoverflow.com/questions/22903572/get-checked-values-of-checkbox-which-is-bind-using-custome-listview-android) – Raghunandan Apr 07 '14 at 11:22

3 Answers3

0

If your using onClickListner in customAdapter the onitemClickListner will not work. i answered question because i can't comment because of repulation

public class CustomListAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;


public CustomListAdapter (Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.layout1, null);

    Checkbox Checkeditem= (Checkbox )vi.findViewById(R.id.checkitem); 


    HashMap<String, String> listitem = new HashMap<String, String>();
    listitem = data.get(position);

    // Setting all values in listview
    Checkeditem.setText(listitem .get(MainActivity.strTagDate));

    return vi;
}}    

In your MainActivity
CustomListAdapter adapter = new CustomListAdapter (Mainactivity.this, aaSubCategory); lvUserInterest.setAdapter(adapter);

W I Z A R D
  • 1,224
  • 3
  • 17
  • 44
0

try this :

first of all you have to set listview to multiple choice like this :

lvUserInterest.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

SparseBooleanArray checked = lvUserInterest.getCheckedItemPositions();

for (int i = 0; i < lvUserInterest.getAdapter().getCount(); i++) {
    if (checked.get(i)) {
        // Do something
    }
}

get your checked values in if condition where you can get checked item...

Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

I have solved my problem by this website : mysamplecode.com/2012/07/android-listview-checkbox-example.html

Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111