使用Custom Listview android检查CheckBox的值

时间:2014-04-07 11:20:50

标签: android listview checkbox

我使用自定义Checkbox绑定ListView。现在,最后,我想要检查CheckBox的所有值。

如何得到这个?我想知道如何使用我的代码获取值。 ListView itemOnClickListener并未在这里开火。

我的代码:

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>

3 个答案:

答案 0 :(得分:0)

如果您在customAdapter中使用onClickListneronitemClickListner将无效。我回答了问题,因为我无法评论,因为他的评论

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

在您的MainActivity中 CustomListAdapter adapter = new CustomListAdapter (Mainactivity.this, aaSubCategory); lvUserInterest.setAdapter(adapter);

答案 1 :(得分:0)

试试这个:

首先,您必须将listview设置为多个选项,如下所示:

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

获取您选中的值,如果您可以获得检查项目的条件......

答案 2 :(得分:0)

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