Android Studio-如何保存在上一个活动中选择的数据 [英] Android studio - How to save data selected on previous activity

查看:381
本文介绍了Android Studio-如何保存在上一个活动中选择的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码的一部分. Textview充当按钮并在其上具有Onclicklistner.单击cpu1000 Textview时,它会导致cpu_g1000类,其代码如下所示.

This is a snippet of my code. The Textview acts as buttons and have Onclicklistner on them. When cpu1000 Textview is clicked it leads to the cpu_g1000 class for which the code is shown below.

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类.然后,该字符串将替换为选择处理器" 以显示新选择.我遇到的问题是,当我选择主板时,处理器的选择将还原为选择处理器" ,并且显示主板选择.我需要同时显示两者.

In this class, there are radio buttons. The users choose one of the choices and the choice is changed to strings. The strings are sent back to Game_1000 class. The string then gets substituted with "Choose a processor" to show the new choice. The issue I am having is when I choose a motherboard the processor choice is reverted back to "Choose a processor" and the motherboard choice shows. I need both to show at the same time.

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

        }

    });
}

已选择处理器:

已选择主板:

选择示例:

先前的选择不会被记录下来,当做出新的选择时,先前的选择会恢复为请选择处理器",但是我需要同时显示这两个信息.在我的原始代码中,我不仅有两个选择,但我将其缩短以使其易于阅读.

The previous choice are not recored and when a new choice is made the previous choice reverts back to "Please choose a processor" but I need both of the information to show. In my original code, I have more than just 2 choices but I shortened it to make it easier to read.

推荐答案

通过调用startActivityForResult(...)开始第二次(选择)活动以获取结果,然后在用户完成交互时,将所选数据设置为Intent并传递该数据意图达到期望结果的方法setResult(...),然后在第二个活动上调用完成.

Start your second (Selection) activity for result by calling startActivityForResult(...) and then when user completes the interaction set your selected data in an Intent and pass that intent to method with desired result setResult(...) then call finish on second activity.

完成第二个活动后,您将通过第一个活动的onActivityResult(...)方法接收意图数据,从意图中提取该数据,然后将其显示给用户.

After finishing the second activity you will receive intent data in onActivityResult(...) method of first activity, extract that data from intent and then show it to user.

更多说明

示例

这篇关于Android Studio-如何保存在上一个活动中选择的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆