更改列表项单击上的复选框状态?

发布于 2024-12-09 07:22:58 字数 3152 浏览 0 评论 0原文

我的每一行都有一个列表和一个复选框。 我希望每当我单击一行时,复选框都会相应地更改其状态。

checkbox.xml

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

单击我的列表的侦听器

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

posts_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:

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

早茶月光 2024-12-16 07:22:58

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

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 事件,则 v 永远不应该为 null,但我已经进行了适当的检查。

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

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

£烟消云散 2024-12-16 07:22:58

在 onListItemClick 内部执行以下操作:

CheckBox cb = (CheckBox) v.findViewById(R.id.CheckBox);
cb.setChecked(!cb.isChecked());

Inside the onListItemClick do this:

CheckBox cb = (CheckBox) v.findViewById(R.id.CheckBox);
cb.setChecked(!cb.isChecked());
恋你朝朝暮暮 2024-12-16 07:22:58
CheckBox checkBox = (CheckBox) v.findViewById(R.id.CheckBox);
checkBox.setChecked(true);
CheckBox checkBox = (CheckBox) v.findViewById(R.id.CheckBox);
checkBox.setChecked(true);
终止放荡 2024-12-16 07:22:58

这是完整的代码,您可以在设置适配器后添加此代码:

list.setOnItemClickListener(new AdapterView.OnItemClickListener(){


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           String message = "Item " + position;
           Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();

            CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
            cb.setChecked(!cb.isChecked());


        }


    });

Here is the full code, you can add this after setting the addapter:

list.setOnItemClickListener(new AdapterView.OnItemClickListener(){


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           String message = "Item " + position;
           Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();

            CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
            cb.setChecked(!cb.isChecked());


        }


    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文