Android Radio buttons in a custom listview changes its state when we scroll the list(当我们滚动列表时,自定义列表视图中的 Android 单选按钮会更改其状态)
问题描述
我是一个初学者,面临一个问题,我正在创建一个带有两个 RadioButton
和一个 TextView
的 ListView
.一切都很好,但是当我设置 RadioButton
并滚动它时,以前的 RadioButton
的值会改变它们的值或显着失去它们的状态.我试图解决这个问题很长时间.我应该做出哪些改变来克服这个问题?
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
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
Option.java
public class Option {
public String title;
public Option(){
super();
}
public Option( String title) {
super();
this.title = title;
}
}
RadioGroupAdapter.java
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
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>
推荐答案
需要为每个item保存RadioButton的状态,所以在滚动的时候首先在 getView() 方法中检查每个 RadioButton 的状态.
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:- 你需要创建一个 Pojo 类(Setter & Getter) 来保存 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
这个例子是一个带有 CheckBox 的 ListView,你需要根据你的要求把它改成 RadioButton.
This example is a ListView with CheckBox you need to change it to RadioButton according to your requirement.
这篇关于当我们滚动列表时,自定义列表视图中的 Android 单选按钮会更改其状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我们滚动列表时,自定义列表视图中的 Android 单选按钮会更改其状态
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01