更改列表项上的复选框状态点击? [英] Change CheckBox state on List item click?

查看:135
本文介绍了更改列表项上的复选框状态点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表和它的每一行的复选框。
我想要每当我点击一行,复选框就会相应地改变它的状态。

I have a list and a checkbox in every row of it. I want that whenever I click on a row, the checkbox changes its state accordingly.

checkbox.xml

checkbox.xml

<CheckBox
    android:id="@+id/CheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:text="" />

点击我的列表中的侦听器

click listener for my list

protected void onListItemClick(ListView l, View v, int position, long id) {
    Toast.makeText(getApplicationContext(), "You have selected item no."
            + (position + 1) + "", Toast.LENGTH_SHORT).show();

    super.onListItemClick(l, v, position, id);
    selectedRow = position;

    if (v == null) {
        LayoutInflater lif = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = lif.inflate(R.layout.posting_detail_question_row, null);
    }

    if (checkedTextView.isChecked()) {
        Toast.makeText(getApplicationContext(), "Already voted",
                Toast.LENGTH_SHORT).show();
    } else {
        String[] from = {"option", "votes"}; // values to fetch from
        // hashmap
        int[] to = {R.id.option, R.id.votes}; // id's for the view

        SimpleAdapter adp = new SimpleAdapter(getApplicationContext(),
                hashList, R.layout.posting_detail_question_row, from, to);
        setListAdapter(adp);
        checkedTextView.setChecked(true);
    }
}

posting_detail_question_row.xml

posting_detail_question_row.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/poll_border_top_bg"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/option"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dp"
            android:layout_weight=".3"
            android:gravity="center_vertical"
            android:text="Answer 1"
            android:textColor="#333333"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/votes"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textColor="#333333"
            android:textSize="15sp" />

        <CheckBox
            android:id="@+id/CheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:text="" />
    </LinearLayout>
</LinearLayout>

我的适配器是一个SimpleAdapter:

My adapter is a SimpleAdapter:

    SimpleAdapter adp = new SimpleAdapter(getApplicationContext(),
            hashList, R.layout.posting_detail_question_row, from, to);
    Log.v("MyLog", "after simple adapter");
    setListAdapter(adp);


推荐答案

您需要使用findViewById来获取指向复选框:

You need to use findViewById to get a pointer to your checkbox:

protected void onListItemClick(ListView l, View v, int position, long id) {

    Toast.makeText(getApplicationContext(), "You have selected item no."
                 +(position+1)+"", Toast.LENGTH_SHORT).show();

    super.onListItemClick(l, v, position, id);

    if (v != null) {
        CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
        checkBox.setChecked(!checkBox.isChecked());
    }
}

如果您收到onClick事件, $ c> v 应该永远不为null,但我已经进行了适当的检查。

If you are recieving the onClick event then v should never be null but I have put on the appropriate checks.

这篇关于更改列表项上的复选框状态点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆