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>
2

3 Answers 3

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...

Sign up to request clarification or add additional context in comments.

6 Comments

I am getting NullPointerException at (checked.get(i))
have your list view contain item or not.
Now, I am not getting error. but how to get its value ?
When I am executing this code, It is going in else condition only.
you have to check you list item after you will in if condition.
|
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);

9 Comments

Do I need to make another class ? then how to use this in main class ?
In main class create object of CustomListAdapter.
like this CustomListAdapter adapter = new CustomListAdapter (Mainactivity.this, aaSubCategory); lvUserInterest.setAdapter(adapter);
ohk, and then what to do when I want all values of checked checkbox ?
ok then your onitemClickListner will work and from that you can get the checked item. thats it
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.