当我们滚动列表和自定义列表视图的Android单选按钮改变其状态 [英] Android Radio buttons in a custom listview changes its state when we scroll the list
问题描述
我是初学者,面对在那里我有两个单选
和一个创建一个的ListView
问题的TextView
。一切顺利,但是当我设置单选
和滚动它,previous 单选
更迭的值他们的价值观或显着失去他们的状态。我试图解决这个问题了很长一段时间。我应该什么样的变化来克服这个问题?
MainActivity.java
公共类MainActivity延伸活动{ 私人ListView控件ListView1的;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 选项weather_data [] =新的选项[]
{
新选项(标题1),
新选项(Heading12),
新选项(Heading3),
新选项(Heading4),
新选项(Heading5),
新选项(Heading6),
新选项(Heading7),
新选项(Heading8),
新选项(Heading9),
新选项(Heading10)
};
RadioGroupAdapter适配器=新RadioGroupAdapter(这一点,
R.layout.listitem,weather_data);
ListView1的=(的ListView)findViewById(R.id.list);
listView1.setAdapter(适配器);
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
}
Option.java
公共类选项{
公共字符串称号;
公共选择(){
超();
} 公共选择(字符串名称){
超();
this.title =称号;
}
}
RadioGroupAdapter.java
公共类RadioGroupAdapter扩展ArrayAdapter<期权及GT; { 上下文语境;
INT layoutResourceId;
选项数据[] = NULL; 公共RadioGroupAdapter(上下文的背景下,INT layoutResourceId,
选项[]数据){
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
MatrixHolder支架=无效; 如果(行== NULL){
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假); 持有人=新MatrixHolder();
holder.txtTitle =(TextView中)row.findViewById(R.id.heading);
holder.group =(RadioGroup中)row.findViewById(R.id.radio_group1);
最后的单选按钮[] = RB新单选[2];
对(INT I = 0; I&2;我++){
RB [I] =新的单选按钮(背景);
//rb[i].setButtonDrawable(R.drawable.single_radio_chice);
RB [I] .setId(ⅰ);
RadioGroup.LayoutParams PARAMS =新RadioGroup.LayoutParams(
0,LayoutParams.WRAP_CONTENT);
params.weight = 1.0F;
params.setMargins(5,0,5,10);
holder.group.addView(RB [I],则params); //的单选按钮被添加到radioGroup中代替的布局
}
row.setTag(保持器);
}其他{
支架=(MatrixHolder)row.getTag();
} 选项option =数据[位置]
holder.txtTitle.setText(option.title);
返回行;
} 静态类MatrixHolder {
TextView的txtTitle;
RadioGroup中组;
INT位置;
}
}
listitem.xml
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向
机器人:weightSum =100> <的TextView
机器人:ID =@ + ID /标题
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =左
机器人:填充=10dp
机器人:TEXTSIZE =18sp
机器人:文字样式=大胆
机器人:layout_weight =50/> < RadioGroup中
机器人:ID =@ + ID / radio_group1
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =center_vertical | CENTER_HORIZONTAL
机器人:方向=横向
机器人:layout_weight =50>
< / RadioGroup中>< / LinearLayout中>
您需要保存单选按钮的状态,每个项目,所以在滚动
在getView()方法,首先它会检查每个单选按钮的状态。
第一步: - 你需要创建一个POJO类(二传手&安培;吸气)用于保存单选按钮的状态
。有关详细信息,请通过下面的链接
的http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html
这例子是复选框您需要根据您的要求将其更改为单选一个ListView。
I am a beginner and facing a problem where I am creating a ListView
with two RadioButton
s and a TextView
. Everything goes fine, but when I set RadioButton
s and scroll it, the values of previous RadioButton
s change their values or lose their state dramatically. I am trying to solve this problem for long time. What changes should I make to overcome this problem?
MainActivity.java
public class MainActivity extends Activity {
private ListView listView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Option weather_data[] = new Option[]
{
new Option("Heading1"),
new Option("Heading12"),
new Option("Heading3"),
new Option("Heading4"),
new Option("Heading5"),
new Option("Heading6"),
new Option("Heading7"),
new Option("Heading8"),
new Option("Heading9"),
new Option("Heading10")
};
RadioGroupAdapter adapter = new RadioGroupAdapter(this,
R.layout.listitem, weather_data);
listView1 = (ListView)findViewById(R.id.list);
listView1.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Option.java
public class Option {
public String title;
public Option(){
super();
}
public Option( String title) {
super();
this.title = title;
}
}
RadioGroupAdapter.java
public class RadioGroupAdapter extends ArrayAdapter<Option> {
Context context;
int layoutResourceId;
Option data[] = null;
public RadioGroupAdapter(Context context, int layoutResourceId,
Option[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MatrixHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MatrixHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.heading);
holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
final RadioButton[] rb = new RadioButton[2];
for(int i=0; i<2; i++){
rb[i] = new RadioButton(context);
//rb[i].setButtonDrawable(R.drawable.single_radio_chice);
rb[i].setId(i);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight=1.0f;
params.setMargins(5, 0, 5, 10);
holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
}
row.setTag(holder);
} else {
holder = (MatrixHolder) row.getTag();
}
Option option = data[position];
holder.txtTitle.setText(option.title);
return row;
}
static class MatrixHolder {
TextView txtTitle;
RadioGroup group;
int position;
}
}
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:id="@+id/heading"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_weight="50" />
<RadioGroup
android:id="@+id/radio_group1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:layout_weight="50" >
</RadioGroup>
</LinearLayout>
You need to save the state of RadioButton for each item, so while scrolling in getView() method first it will check the state of each RadioButton.
Step1:- You need to create a Pojo class(Setter & Getter) for saving the state of RadioButton.
For more details go through the below link
http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html
This example is a ListView with CheckBox you need to change it to RadioButton according to your requirement.
这篇关于当我们滚动列表和自定义列表视图的Android单选按钮改变其状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!