问题描述:

I'm very new to Android programming, and I'm unable to find out what the accepted way to reach my goal is:

I have a variable in my main activity (sortOrder), which I want to set using radio buttons in a different activity. At the moment, I just change MainActivity.sortOrder from the other activity with a big switch case, so I'm wondering if there is a more elegant way.

Also, at the moment the radio buttons "forget" their choice when I return to MainActivity. Is is possible to save the selected radio button, and set the variable from that in MainActivity?

Here's some code:

in MainActivity:

public static String sortOrder;

public static int checkedId;

These are given values in onCreate:

sortOrder = ShipContract.ShipEntry.COLUMN_NAME_NAME + " ASC";

checkedId = R.id.sort_name;

And here is OptionsSort, which is started from MainActivity:

public class OptionsSort extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_options_sort);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

RadioButton checkedButton = (RadioButton) findViewById(MainActivity.checkedId);

checkedButton.setChecked(true);

}

public void onRadioButtonClicked(View view) {

boolean checked = ((RadioButton) view).isChecked();

switch(view.getId()) {

case R.id.sort_name:

if (checked)

MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_NAME + " ASC";

break;

case R.id.sort_date:

if (checked)

MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_DATE + " ASC";

break;

case R.id.sort_capacity:

if (checked)

MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_CAPACITY + " ASC";

break;

case R.id.sort_line:

if (checked)

MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_LINE + " ASC";

break;

case R.id.sort_displacement:

if (checked)

MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_DISPLACEMENT + " ASC";

break;

case R.id.sort_speed:

if (checked)

MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_SPEED + " ASC";

break;

case R.id.sort_power:

if (checked)

MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_POWER + " ASC";

break;

}

RadioGroup radiogroup = (RadioGroup) findViewById(R.id.sort_group);

MainActivity.checkedId = radiogroup.getCheckedRadioButtonId();

finish();

}

}

网友答案:

so far i understand your problem you want to take sortOder value from activity B and bring it to Activity A.This can be achieved if you startActivityForResult and there in activity B when you are done with everything just call setresult method and give it the resultant intent and finish this activity B. In activity A you have to override onActivityResult and handle the result from activity B .

like startActivityForResult(new Intent(this,ActivityB.class),123);

@Override

public void onActivityResult(int requestCode, int resultCode, Intentdata) { super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {

if (requestCode==123)
    {
      String sordData= data.getStringExtra("sortOrder","byName")
            sortYourDAta(sordData);
    }
}

}

and in activity b when sort order is selected

just store value in a variable sortOrder="byDate";

and onbackpressed or when ever you want to go back just call this setResult(123,new Intent().putExtra("sortOrder",sortOrder)); finish();

相关阅读:
Top