【问题标题】:Change CheckBox state on List item click?单击列表项更改复选框状态?
【发布时间】:2023-03-31 13:38:01
【问题描述】:

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

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

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:

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

【问题讨论】:

  • 在此处发布您的 posts_detail_question_row.xml 布局代码
  • @PM-Paresh Mayani 先生,现在检查一下
  • @Lalit Poptani,先生,我的适配器是简单的适配器,我猜这东西只适用于阵列适配器

标签: android listview checkbox


【解决方案1】:

在 onListItemClick 里面这样做:

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

【讨论】:

    【解决方案2】:

    这里是完整的代码,你可以在设置 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());
    
    
            }
    
    
        });
    

    【讨论】:

      【解决方案3】:

      您需要使用 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 绝不应该为空,但我已经进行了适当的检查。

      【讨论】:

      • @Merlin 这不起作用先生,它显示运行时错误......我会再次改写我的问题......我希望当我点击列表的任何行时,复选框应该相应地检查
      • 我发现了一个小错误,应该是R.id.Checkbox,但我相信你发现了……你能看看 logcat 看看错误是什么
      • @ Merlin 你的回答虽然让我知道我错过了什么,即 (v!=null) 部分但仍然卡住,如果我按照你的做法,我的问题得到了解决,但它在什么时候起作用我点击了两次,因为我的适配器是在 else 子句中创建的,并且它缺少复选框...我已经更新了代码你可以看看它并给我一些建议..谢谢你的回答..
      • 我想我现在明白你的意思了......你应该在 onCreate 中调用 setListAdapter。当您想要更新数据并在列表适配器上调用 notifyDataSetChanged 以更新列表视图时。您的 listadapter 类应该设置复选框的值
      • @ merlin,我想问题出在我的简单适配器上,因为当我投票/单击时,每次都会创建一个新适配器,使我的视图变为空......你怎么看??
      【解决方案4】:
      CheckBox checkBox = (CheckBox) v.findViewById(R.id.CheckBox);
      checkBox.setChecked(true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 1970-01-01
        • 2020-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多