在Type类的ArrayAdapter中保存单选按钮的状态

时间:2012-12-28 09:27:08

标签: java android listview radio-button savestate

当用户点击后退键然后返回活动时,我遇到了保存所选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;
  }

当它实际选择了一个单选按钮时,我很兴奋。但问题是它只选择列表中的第一个按钮,即使我取消选择它并选择了另一个项目。此外,它让我选择了一个我不想要的单选按钮。我错过了什么?

2 个答案:

答案 0 :(得分:0)

创建一个布尔数组并用假值填充它作为单选按钮的数量。现在,如果单击单选按钮,则使该索引为true(如果为false),如果再次单击则将其设置为false。使用您自己的列表适配器,它将帮助您更多。我在复选框中做了类似的事情,你可以看到我的帖子Unable to check/uncheck CheckedTextView inside getView。如果这有助于你,请回复我......

答案 1 :(得分:0)

当您按下后退键时使用意图导航时,您正在丢失无线电组的状态,因此重新初始化活动(即再次当您进入屏幕时它将从oncreate开始),您需要添加额外内容意图向您发送单选按钮状态

像这样的例子

 @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);
}

并且在ProfileMaker活动检查中是否有任何额外的,如果有额外的获取,并根据它选择radiobutton。

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