I'm having troubles saving the state of a selected radiobutton
when the user hits the back key and then comes back to the activity. My class extends ListActivity
. Here's the onCreate
and onBackPressed()
methods of the class that creates the 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);
}
Whenever I press the home button and come back to the application, the radiobutton
that I selected is still selected, but when I press the back key to go to the previous activity and then come back to this one, the radiobutton
is not selected. How can I save the state of a previously selected radiobutton
? These radiobuttons
are in a radiogroup
. Any help is highly appreciated. Thanks.
EDIT: Ok, so this is what I got.
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;
}
I got excited when it actually had a radiobutton selected. But the problem is it only selects the first button in the list an nothing else even if I deselected it and selected another item. Also, it's letting me select more than one radio button which I don't want. What am I missing?