单选按钮未 selected 而列表视图中的 select 单选按钮

radiobutton is not selected while select radiobutton in listview

我在单选组中创建了带有两个单选按钮的自定义列表视图。

我的代码getter和setter文件

Item_radio.java

package com.example.customlistview_radio;

public class Item_radio 
{
    public boolean isSelected_radio1;
    public boolean isSelected_radio2;

    public Item_radio(boolean radio1_status,boolean radio2_status)
    {
        this.isSelected_radio1=radio1_status;
        this.isSelected_radio2=radio2_status;
    }

    public boolean isSelected_radio1() {
        return isSelected_radio1;
    }
    public void setSelected_radio1(boolean isSelected_radio1) {
        this.isSelected_radio1 = isSelected_radio1;
    }
    public boolean isSelected_radio2() {
        return isSelected_radio2;
    }
    public void setSelected_radio2(boolean isSelected_radio2) {
        this.isSelected_radio2 = isSelected_radio2;
    }
}

MyRadioAdapter.java

package com.example.customlistview_radio;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;

public class MyRadioAdapter extends BaseAdapter 
{
    Context context;
    List<Item_radio> radioList;
    public MyRadioAdapter(Context context,List<Item_radio> radiolist)
    {
        this.context=context;
        this.radioList=new ArrayList<Item_radio>();
        this.radioList.addAll(radiolist);
    }

    @Override
    public int getCount() {
        return radioList.size();
    }

    @Override
    public Object getItem(int position) {
        return radioList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder
    {
        RadioButton radio_button1;
        RadioButton radio_button2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        final ViewHolder holder;
        View view = convertView;

        if(view==null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            view = inflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();

            holder.radio_button1=(RadioButton)view.findViewById(R.id.my_radio_but1);
            holder.radio_button2=(RadioButton)view.findViewById(R.id.my_radio_but2);

            view.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) view.getTag();
        }

        if (position % 2 == 1) 
        {
            view.setBackgroundColor(Color.CYAN);
        }
        else
        {
            view.setBackgroundColor(Color.WHITE);
        }

        final Item_radio radio = (Item_radio) getItem(position);
        holder.radio_button1.setChecked(radio.isSelected_radio1());
        holder.radio_button2.setChecked(radio.isSelected_radio2());

        holder.radio_button1.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {

                Log.e("button1","Clicked:button1");
                RadioButton rb=(RadioButton)v;
                radio.setSelected_radio1(rb.isChecked());
                Log.e("button1","Clicked:button1:"+rb.isChecked());
            }
        });
        holder.radio_button2.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {       
                RadioButton rb=(RadioButton)v;
                Log.e("button2","Clicked:button2"+rb.isChecked());
                radio.setSelected_radio2(rb.isChecked());
            }
        });
        return view;
    }

}

MainActivity.java

package com.example.customlistview_radio;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity 

{

    ListView mylist;
    Button submit_but;

    ArrayList<Item_radio> radio_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mylist=(ListView)findViewById(R.id.mylist_demo);
        submit_but=(Button)findViewById(R.id.submit_but);


        radio_list=new ArrayList<Item_radio>();

        for(int i=0;i<20;i++)
        {
            radio_list.add(new Item_radio(false,false));
        }

        mylist.setAdapter(new MyRadioAdapter(MainActivity.this, radio_list));   
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <RadioGroup
         android:id="@+id/my_radio_group"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
      >

         <RadioButton
             android:id="@+id/my_radio_but1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="radio1" />

         <RadioButton
             android:id="@+id/my_radio_but2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="radio2" />
     </RadioGroup>

</RelativeLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:orientation="vertical"
    tools:context="com.example.customlistview_radio.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />

    <ListView
        android:id="@+id/mylist_demo"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice"
        tools:listitem="@layout/list_item"
        android:layout_weight="0.3" >
    </ListView>

    <Button 
        android:id="@+id/submit_but"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="submit"
        android:layout_weight="1.5"/>

</LinearLayout>

当 select 编辑列表行中的单选按钮时,某些单选按钮被 select 编辑。

如果我滚动列表视图和 select 最后 5 或 6 个项目之间的单选按钮,它将不会 selected。

问题

那是假设单选按钮 1 没有 selected 然后我选择 radiobutton2 它得到 selected 之后 radiobutton1 是 selected.

我的logCat

03-04 10:22:17.221: E/button2(4665): Clicked:button2true
03-04 10:22:20.201: E/button2(4665): Clicked:button2true
03-04 10:22:21.221: E/button2(4665): Clicked:button2true
03-04 10:22:22.101: E/button2(4665): Clicked:button2true
03-04 10:22:23.711: E/button2(4665): Clicked:button2true
03-04 10:22:24.671: E/button2(4665): Clicked:button2true
03-04 10:22:25.711: E/button2(4665): Clicked:button2true
03-04 10:22:40.770: E/button2(4665): Clicked:button2true
03-04 10:22:42.371: E/button2(4665): Clicked:button2false
03-04 10:22:52.271: E/button2(4665): Clicked:button2false

请查看我的位置,前 8 行工作正常。 当我单击第 9 行中的 radiobutton2 时,selected 打印检查状态为 false 但我 select 按钮和检查的外观在 unselected 模式下仍未更改。

我做错了什么吗? 请帮我解决这个问题。 谢谢...

您需要稍微更改一下代码。

第 1 步:

像这样在您的适配器中创建 HashMap

HashMap<Integer,Integer> selectedItems  = new HashMap<Integer, Integer>();

第 2 步: 在 getView 内部添加以下内容

if ( selectedItems.get( position ) != null ) {
                 holder.radio_button1.setChecked(radio.isSelected_radio1()); selectedItems.get( position ) );
             }

第 3 步: 在单选按钮 onlick 中添加以下内容

selectedItems.put( position, your_value);

注:

这里your_value是0或者1,如果1表示单选按钮被选中,0表示没有被选中。

试试这个,如果遇到任何问题,请告诉我。

我只是为单选按钮 1 添加这个。先试试这个。如果有效,请修改您的单选按钮 2

编辑 1:

你为什么不试试像下面这样的 Radiogroup?????

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);



            }

        });

一个有时能用,但之后就不行了,这都是因为你在其余代码之前使用了 setChecked。所以我可以给你的建议是删除这些行

  holder.radio_button1.setChecked(radio.isSelected_radio1());
  holder.radio_button2.setChecked(radio.isSelected_radio2());

更新:如果这会损害您的列表,请执行以下操作

在这里把你的逻辑倒过来,设置radio_1勾选后,设置radio_2不勾选。您的代码如下所示

   holder.radio_button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Log.e("button1", "Clicked:button1");
            RadioButton rb = (RadioButton) v;

            radio.setSelected_radio1(rb.isChecked());
            radio.setSelected_radio2(!rb.isSelected());
            Log.e("button1", "Clicked:button1:" + rb.isChecked());
        }
    });
    holder.radio_button2.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            RadioButton rb=(RadioButton)v;
            Log.e("button2","Clicked:button2"+rb.isChecked());
            radio.setSelected_radio2(rb.isChecked());
            radio.setSelected_radio1(!rb.isSelected());
        }
    });

更新 2

无论当前发生什么,都是因为您的视图被回收了,您可以使用的最后一个选项是,删除此 if(view==null) 条件并将 else 子句与其代码匹配。