Question

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?

Était-ce utile?

La solution

You are losing the state of the radiogroup as you are navigating using intent when the back key is pressed, so activity reinitializing (i.e Again when you come to your screen it will start from oncreate) , you need to add extras to your intent send you radio button states

example like this

 @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    Intent newIntent = new Intent(ProfileMaker.this, Monitor.class);
newIntent.putExtra("somename",state of the radiobutton);
    startActivity(newIntent);
}

and in ProfileMaker activity check is there any extra , if there is an extra get that and select radiobutton according to that.

if (getIntent().getExtras().getString("yourextraname") != null)
            applicant = getIntent().getExtras().getString("applicant");

Autres conseils

Make an boolean array and fill it with false value as number of radio buttons.Now when ever you click radio button make that index true if it was false and if clicked again do it false. Use your own list adapter it will helps you more . I had done similar in case of check box you can see my post Unable to check/uncheck CheckedTextView inside getView . Reply me if this helps you ...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top