[英]Android studio - How to save data selected on previous activity
這是我的代碼片段。 Textview
充當按鈕並在Onclicklistner
上具有Onclicklistner
。 單擊 cpu1000 Textview
,它會指向cpu_g1000
類,其代碼如下所示。
public class Game_1000 extends AppCompatActivity implements View.OnClickListener{
private TextView cpu1000, mobo1000;
TextView cpu, mobo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_1000);
cpu1000 = (TextView) findViewById(R.id.proName_G1);
mobo1000 = (TextView) findViewById(R.id.moboName_G1);
cpu1000.setOnClickListener(this);
mobo1000.setOnClickListener(this);
cpu = (TextView) findViewById(R.id.proNameG1000);
cpu.setText(getIntent().getStringExtra("Processor"));
mobo = (TextView) findViewById(R.id.moboNameG1000);
mobo.setText(getIntent().getStringExtra("Motherboard"));
}
@Override
public void onClick(View v) {
if (v == cpu1000) {
opencpu_g1000();
}
else if (v == mobo1000) {
openmobo_g1000();
}
}
public void opencpu_g1000() {
Intent intent = new Intent(this, cpu_g1000.class);
startActivity(intent);
}
public void openmobo_g1000() {
Intent intent = new Intent(this, mobo_g1000.class);
startActivity(intent);
}
在這個類中,有單選按鈕。 用戶選擇其中一個選項,然后選項更改為字符串。 字符串被發送回Game_1000
類。 然后該字符串被替換為“選擇處理器”以顯示新選擇。 我遇到的問題是,當我選擇主板時,處理器選擇會恢復為“選擇處理器”並顯示主板選擇。 我需要同時顯示兩者。
public class cpu_g1000 extends AppCompatActivity {
Button button_save;
RadioGroup rG;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cpu_g1000);
button_save = (Button) findViewById(R.id.Save_G1_cpu);
rG = (RadioGroup) findViewById(R.id.cpu_RadioGrp);
tv = (TextView) findViewById(R.id.proNameG1000);
button_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selected_cpu = rG.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu);
String radioValue = selectedRadioButton.getText().toString();
Intent intent = new Intent(cpu_g1000.this, Game_1000.class);
intent.putExtra("Processor", radioValue);
startActivity(intent);
}
});
}
處理器選擇:
主板選擇:
選擇示例:
之前的選擇不會被記錄,當做出新的選擇時,之前的選擇會恢復為“請選擇處理器”,但我需要顯示這兩個信息。 在我的原始代碼中,我有不止 2 個選擇,但我縮短了它以使其更易於閱讀。
對於您的場景,您需要做的是保存處理器和主板選擇結果或將數據從一個活動發送到另一個活動。 在處理器選擇之后,您將返回Game_1000
Activity,您可以使用startActivityForResult 。 此方法將結果發送回通過 Intent 調用 Activity。 這意味着如果您從Game_1000
Activity 調用cpu_g1000
Activity 並且在選擇 Processor 后您可以將結果發送回Game_1000
Activity。 Game_1000
Activity 將在onActivityResult
方法中獲取結果。
例子:
開始cpu_g1000
活動與startActivityForResult從Game_1000
活動。
public void opencpu_g1000() {
Intent intent = new Intent(this, cpu_g1000.class);
startActivityForResult(intent, PROCESSOR_SELECTION_CODE);
}
在cpu_g1000
Activity 中將結果設置為 Intent 並發送回Game_1000
Activity
button_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selected_cpu = rG.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu);
String radioValue = selectedRadioButton.getText().toString();
Intent resultIntent = new Intent();
// TODO Add data to send back
resultIntent.putExtra("ProcessorSelection", radioValue);
setResult(Activity.RESULT_OK, resultIntent);
// finishing this activity
finish();
}
});
在Game_1000
活動得到發送的值形式cpu_g1000
通過重寫活動onActivityResult
方法。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (PROCESSOR_SELECTION_CODE) : {
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.
String radioValue = data.getStringExtra("ProcessorSelection");
// save the result value and update UI
}
break;
}
}
}
對於mobo_g1000
Activity,使用相同的程序將結果返回到Game_1000
Activity。 使用startActivityForResult啟動mobo_g1000
Activity 並獲取Game_1000
Activity 的onActivityResult
方法中的值。 您必須在onActivityResult
方法中為mobo_g1000
Activity 添加另一個case
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.