简体   繁体   中英

Checked values of CheckBox with Custom Listview android

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>

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);

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

我已经通过以下网站解决了我的问题: mysamplecode.com/2012/07/android-listview-checkbox-example.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM