无法将数据从第二个活动传递到主要活动

时间:2018-10-01 10:43:48

标签: java android

我无法在Activity2和ActivityMain之间传递数据。 我有一个带有“添加人”按钮的ActivityMain。当我按下此按钮时,我进入“ Activity2”,然后填写“名称”字段并按下“添加”按钮,但是无法在我的ActivityMain上添加名称。

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add Person"
    android:onClick="addPerson"
   />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

ActivityMain.java

package com.example.test.zadanie01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; 
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        Intent intent = getIntent();
        String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
        setContentView(textView);
}

public void addPerson(View view){
    Intent intent = new Intent(this, PersonActivity.class);
    startActivityForResult(intent, 1);
}
}

Activity2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/send_name"/>
   <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Person"
        android:onClick="addPersonToMain"
        />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Activity2.java

package com.example.test.zadanie01;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class PersonActivity extends AppCompatActivity {

public final static String EXTRA_MESSAGE = "EXTRA_MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_person);
}

public void addPersonToMain(View view){
    Intent returnintent = new Intent(this, MainActivity.class);
    EditText SendName = (EditText) findViewById(R.id.send_name);

    String message = SendName.getText().toString();
    returnintent.putExtra(EXTRA_MESSAGE, message);
    setResult(RESULT_OK, returnintent);
    finish();
}
}

2 个答案:

答案 0 :(得分:-1)

更改代码的这一部分。

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == 1){
            if (resultCode == RESULT_OK){
                String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);

                TextView textView = new TextView(this);
                textView.setTextSize(40);
                textView.setText(message);
                setContentView(textView);
            }
        }
}

您无需在Intent中创建另一个onActivityResult,而Intent已通过方法Intent data传递。然后将数据用作您的意图。并首先检查它是否是您要求的requestCode,以及结果是否正确。

编辑

从评论中回答问题的另一部分。

1-将TextView添加到您的activity_main.xml中,并将其可见性默认设置为不显示。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
<TextView
 android:id="@+id/text_message"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="40sp"
 android:visibility="gone" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add Person"
    android:onClick="addPerson"
   />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

2-检索数据消息时,显示TextView并显示内容。

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == 1){
            if (resultCode == RESULT_OK){
                String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);

                TextView textView = findViewById(R.id.text_message);
                textView.setVisibility(View.VISIBLE);
                textView.setText(message);
            }
        }
}

答案 1 :(得分:-1)

尝试替换

String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);

使用

String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);