java - 尝试 Intent 额外将字符串值恢复到 TextView 中

标签 java android

我对 android studio 编程还很陌生,似乎无法让我的应用程序将 Intent 值恢复到 TextView 中。

我有一个问题 Activity ,向用户询问一些问题,使用是/否单选按钮回答这些问题。从这里已经进行了高级诊断,因此诊断结果存储在问题 Activity 中的“storeintent()”方法中。问题是,当使用“restoreintents()”方法将 Intent 调用到答案 Activity 时, TextView 中不会显示任何内容。

这是问题 Activity (答案已存储,请查看评论):

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class Questions extends AppCompatActivity {

private Button button2;

private RadioButton radioButton;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioButton radioButton4;
private RadioButton radioButton5;
private RadioButton radioButton6;
private RadioButton radioButton7;
private RadioButton radioButton8;
private RadioButton radioButton9;
private RadioButton radioButton10;
private RadioButton radioButton11;
private RadioButton radioButton12;
private RadioButton radioButton13;
private RadioButton radioButton14;
private RadioButton radioButton15;
private RadioButton radioButton16;
private RadioButton radioButton17;
private RadioButton radioButton18;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_questions);

    storeintent();

    button2 = findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            opendiagnosis();
            storeintent();
        }
    });

    //Question 1
    radioButton = findViewById(R.id.radioButton); //yes
    radioButton2 = findViewById(R.id.radioButton20); //no
    //Question 2
    radioButton3 = findViewById(R.id.radioButton4); //yes
    radioButton4 = findViewById(R.id.radioButton2); //no
    //Question 3
    radioButton5 = findViewById(R.id.radioButton6); //yes
    radioButton6 = findViewById(R.id.radioButton5); //no
    //Question 4
    radioButton7 = findViewById(R.id.radioButton7); //yes
    radioButton8 = findViewById(R.id.radioButton8); //no
    //Question 5
    radioButton9 = findViewById(R.id.radioButton9); //yes
    radioButton10 = findViewById(R.id.radioButton10); //no
    //Question 6
    radioButton11 = findViewById(R.id.radioButton12); //yes
    radioButton12 = findViewById(R.id.radioButton11); //no
    //Question 7
    radioButton13 = findViewById(R.id.radioButton13); //yes
    radioButton14 = findViewById(R.id.radioButton14); //no
    //Question 8
    radioButton15 = findViewById(R.id.radioButton15); //yes
    radioButton16 = findViewById(R.id.radioButton16); //no
    //Question 9
    radioButton17 = findViewById(R.id.radioButton17); //yes
    radioButton18 = findViewById(R.id.radioButton18); //no
}

public void storeintent() {

    String value="Android Studio Is TERRIBLE"; //my string to be displayed in textView16 in the answers activity
    Intent i = new Intent(this, Questions.class);
    i.putExtra("key",value);
    startActivity(i);
}

public void opendiagnosis() {
    // for all pairs: one of each pair has to be checked
    boolean shouldStartNextActivity = (radioButton.isChecked() || radioButton2.isChecked())
            && (radioButton3.isChecked() || radioButton4.isChecked()) && (radioButton5.isChecked() || radioButton6.isChecked())
            && (radioButton7.isChecked() || radioButton8.isChecked())&& (radioButton9.isChecked() || radioButton10.isChecked())
            && (radioButton11.isChecked() || radioButton12.isChecked())&& (radioButton13.isChecked() || radioButton14.isChecked())
            && (radioButton15.isChecked() || radioButton16.isChecked())&& (radioButton17.isChecked() || radioButton18.isChecked());


    if (shouldStartNextActivity){
        Intent intent = new Intent(this, answers.class);
        startActivity(intent);
    } else{
        Toast.makeText(getBaseContext(), "Please answer all the questions for an accurate diagnosis", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);


    savedInstanceState.putBoolean("myOption1", radioButton.isChecked());
    savedInstanceState.putBoolean("myOption2", radioButton2.isChecked());
    savedInstanceState.putBoolean("myOption3", radioButton3.isChecked());
    savedInstanceState.putBoolean("myOption4", radioButton4.isChecked());
    savedInstanceState.putBoolean("myOption5", radioButton5.isChecked());
    savedInstanceState.putBoolean("myOption6", radioButton6.isChecked());
    savedInstanceState.putBoolean("myOption7", radioButton7.isChecked());
    savedInstanceState.putBoolean("myOption8", radioButton8.isChecked());
    savedInstanceState.putBoolean("myOption9", radioButton9.isChecked());
    savedInstanceState.putBoolean("myOption10", radioButton10.isChecked());
    savedInstanceState.putBoolean("myOption11", radioButton11.isChecked());
    savedInstanceState.putBoolean("myOption12", radioButton12.isChecked());
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    radioButton.setChecked(savedInstanceState.getBoolean("myOption1"));
    radioButton2.setChecked(savedInstanceState.getBoolean("myOption2"));
    radioButton3.setChecked(savedInstanceState.getBoolean("myOption3"));
    radioButton4.setChecked(savedInstanceState.getBoolean("myOption4"));
    radioButton5.setChecked(savedInstanceState.getBoolean("myOption5"));
    radioButton6.setChecked(savedInstanceState.getBoolean("myOption6"));
    radioButton7.setChecked(savedInstanceState.getBoolean("myOption7"));
    radioButton8.setChecked(savedInstanceState.getBoolean("myOption8"));
    radioButton9.setChecked(savedInstanceState.getBoolean("myOption9"));
    radioButton10.setChecked(savedInstanceState.getBoolean("myOption10"));
    radioButton11.setChecked(savedInstanceState.getBoolean("myOption11"));
    radioButton12.setChecked(savedInstanceState.getBoolean("myOption12"));

}
}

这是“答案” Activity (其中诊断旨在通过调用 Intent 来检索,但事实并非如此,请参阅所做的评论):

public class answers extends AppCompatActivity {

private Button button7;


private TextView textView;
private TextView textView2;
private TextView textView3;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_answers);

    restoreintents(); //method CALLED HERE!

    button7 = findViewById(R.id.button7);
    button7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            reset();
        }
    });


    textView = findViewById(R.id.textView16);
    textView2 = findViewById(R.id.textView17);
    textView3 = findViewById(R.id.textView18);
}

public void restoreintents() {

    Bundle extras = getIntent().getExtras(); //Questions intent is called here again
    if (extras != null) {
        String value = extras.getString("key");
        //The key argument here must match that used in the other activity

        textView.setText(value); //String does not appear in textbox, it stays empty
    }
}


public void reset() {
    Intent intent = new Intent(this, Questions.class);
    startActivity(intent);
}

}

一些帮助将不胜感激!

最佳答案

为什么不使用intent.putExtra(key, value),然后在目标 Activity 中获取这些发送的信息,并使用 getExtra(key) 函数获取它们。 你可以这样做:

Intent i = new Intent(context, MyActivity.class);   
String value  = "some string";
i.putExtra("KEY_STRING", value );

并在 MyActivity 中获取它:

Bundle bundle = getIntent().getExtras();
if(bundle.getString("KEY_STRING")!= null) {
        String str = intent.getStringExtra("KEY_STRING");
}

关于java - 尝试 Intent 额外将字符串值恢复到 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59947806/

相关文章:

java - 在没有 RMI 或套接字的情况下如何让两个 JVM 进行通信?

java - 通过 2 个点画一条无限长的线?

java - 为什么 Instant.now() 有时格式不同?

android - 为什么在应用程序崩溃或强制关闭 Android 时已重置共享首选项

android - 如何删除 KitKat 的 ActionBar 阴影?

java - Android: java.lang.NumberFormatException: Invalid int: "27.0"

java - Firebase 存储 Android : How can I change the the filename of a downloaded file?

java - 依赖项不应指向项目目录中的文件

java - 如何在单个 Controller 中制作多个@PatchMapping?

android - 如何在 Android 中调整操作栏菜单文本大小