我在radiogroup中使用两个radiobutton创建了自定义列表视图。
我的代码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>
在列表行中选择单选按钮时,会选择一些单选按钮。
如果我滚动列表视图并选择最后5或6项之间的单选按钮,则不会选中它。
问题
假设没有选择单选按钮1,那么我选择radiobutton2然后选择了radiobutton1。
我的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时没有被选中打印检查状态为false但是我选择了按钮,并且检查的外观在未选择模式下仍未更改。
我做错了吗? 请帮我解决问题。 谢谢......
答案 0 :(得分:0)
您需要稍微更改一下代码。
第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步: 在单选按钮内单击添加以下
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);
}
});
答案 1 :(得分:0)
一个人有时会工作,之后也不会工作,这都是因为你在其余的代码之前使用了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
子句与其代码匹配。