radiobutton
当用户点击后退键然后返回活动时,我无法保存选定的状态。我的课扩展了ListActivity
。这是onCreate
创建.onBackPressed()
listview
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_manager);
dataInfo = new HandleData(ProfileMaker.this);
dataInfo.open();
people = dataInfo.getAllComments();
userAdapter = new ArrayAdapter<People>(ProfileMaker.this, android.R.layout.simple_list_item_single_choice, people);
setListAdapter(userAdapter);
registerForContextMenu(getListView());
addUser = (Button) findViewById(R.id.buttonAddUser);
graphUser = (Button) findViewById(R.id.buttonGraph);
addUser.setOnClickListener(this);
graphUser.setOnClickListener(this);
idSharing = getSharedPreferences(idPref, 0);
nameSharing = getSharedPreferences(namePref, 0);
ageSharing = getSharedPreferences(agePref, 0);
weightSharing = getSharedPreferences(weightPref, 0);
genderSharing = getSharedPreferences(genderPref, 0);
otherName = getSharedPreferences(nameThere, 0);
otherAge = getSharedPreferences(ageThere, 0);
otherWeight = getSharedPreferences(weightThere, 0);
otherGender = getSharedPreferences(genderThere, 0);
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent newIntent = new Intent(ProfileMaker.this, Monitor.class);
startActivity(newIntent);
}
每当我按下主页按钮并返回应用程序时radiobutton
,我选择的那个仍然被选中,但是当我按下返回键去上一个活动然后回到这个活动时,radiobutton
没有被选中。如何保存先前选择的状态radiobutton
?这些radiobuttons
在一个radiogroup
. 非常感谢任何帮助。谢谢。
编辑:好的,这就是我得到的。
public class AdapterClass extends ArrayAdapter<People>{
private final List<People> list;
private final Activity context;
public AdapterClass(Activity context, List<People> list) {
super(context, R.layout.profile_manager, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected RadioButton radioButton;
RadioGroup group;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.button_layout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.radioButton = (RadioButton) view.findViewById(R.id.check);
viewHolder.radioButton
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
People element = (People) viewHolder.radioButton
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.radioButton.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).radioButton.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.radioButton.setChecked(list.get(position).isSelected());
return view;
}
当它实际上选择了一个单选按钮时,我很兴奋。但问题是它只选择列表中的第一个按钮,即使我取消选择它并选择了另一个项目。此外,它让我可以选择多个我不想要的单选按钮。我错过了什么?