我是初学者并尝试使用在列表视图中动态生成的无线电组创建列表视图。但是当我滚动列表视图时,首先选择的单选按钮突然改变了他们的选择,其他单选按钮被选中。为什么这会发生呢?如何创建列表,滚动列表后单选按钮的选择不会改变?我正在使用自定义适配器列表视图。我的代码是,
MainActivity.java
package com.list.radiogroup;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
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"),
new Option("Heading11"),
new Option("Heading13")
};
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;
}
}
RadioGroupAdapter.java
package com.list.radiogroup;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class RadioGroupAdapter extends ArrayAdapter<Option> {
Context context;
int layoutResourceId;
Option data[] = null;
private List<Option> list;
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 = null;
MatrixHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MatrixHolder(row);
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].setId(i);
if(!list.get(i).isChecked){
list.get(i).isChecked=false; // more implement here or may be this work
break;
}
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight=1.0f;
params.setMargins(5, 0, 5, 5);
holder.group.addView(rb[i],params);
}
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=null;
int position;
MatrixHolder(View row) {
this.group=(RadioGroup)row.findViewById(R.id.radio_group1);
}
}
}
Option.java
package com.list.radiogroup;
public class Option {
public String title;
boolean isChecked = false;
public Option(){
super();
}
public Option( String title) {
super();
this.title = title;
}
}
答案 0 :(得分:1)
我在列表视图中的复选框也遇到了同样的问题。然后我发现它是因为列表视图回收而发生的(Android有一个回收机制来节省内存)。 您可以在Google中搜索Android列表视图回收问题。
我用这个例子解决了它
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html
我希望它会有所帮助