Checked values of CheckBox with Custom Listview android

耗尽温柔 提交于 2021-01-29 22:12:34

问题


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>

回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/22910936/checked-values-of-checkbox-with-custom-listview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!