0

Hi i have 2 Activitys in my app and i would take the data from the first activity and insert them on the second one.

Pics: I have 2 datepickers to select date range and i save it on texView and texView2.

enter image description here enter image description here

Code: Main Activity

selectDate = (Button) findViewById(R.id.button); date = (TextView) findViewById(R.id.textView);

    selectDate2 = (Button) findViewById(R.id.button2);
    date2 = (TextView) findViewById(R.id.textView2);

    selectDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            datePickerDialog = new DatePickerDialog(MainActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                            date.setText("Entrada: "+day + "/" + (month + 1) + "/" + year);
                        }
                    }, year, month, dayOfMonth);
            datePickerDialog.show();
        }
    });


    selectDate2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            calendar2 = calendar2.getInstance();
            year2 = calendar2.get(Calendar.YEAR);
            month2 = calendar2.get(Calendar.MONTH);
            dayOfMonth2 = calendar2.get(Calendar.DAY_OF_MONTH);
            datePickerDialog2 = new DatePickerDialog(MainActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                            date2.setText("Salida: "+day + "/" + (month + 1) + "/" + year);
                        }
                    }, year2, month2, dayOfMonth2);
            datePickerDialog2.show();
        }
    });

Activity2:

package com.example.mand.consumirapirest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class json extends AppCompatActivity {

    TextView tvJSON;
    TextView textView;

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

    public void setTextView() {
        tvJSON = (TextView) findViewById(R.id.tvJSON);
        textView = (TextView) findViewById(R.id.textView);

        String s = textView.getText().toString();

        tvJSON.setText(s);


        //String text = textView.getText().toString();
        //return text;
    }

    /*public String getText()
    {

    }*/
}

Any idea thanks..!

  • Possible duplicate of [How to pass value of one TextView to another TextView in different Activity](https://stackoverflow.com/questions/10023694/how-to-pass-value-of-one-textview-to-another-textview-in-different-activity) – Android Mar 12 '19 at 08:49

4 Answers4

1

Somewhere in first activity (MainActivity.java):

Button button = findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);

        // Pass some data
        // Key "DATE_KEY" is static field from SecondActivity
        intent.putExtra(DATE_KEY, "20.03.2019");

        startActivity(intent);
    }
});

And in second activity (SecondActivity.java):

public class SecondActivity extends AppCompatActivity {

    static final String DATE_KEY = "DATE_KEY";

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

        // Read passed data - String "20.03.2019"
        String date = getIntent().getStringExtra(DATE_KEY);
    }
}
Boken
  • 4,825
  • 10
  • 32
  • 42
1

Try this

In your MainActivity

buscarButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

   startActivity(new Intent(MainActivity.this, json.class).putExtra("extra1",date.getText().toString()).putExtra("extra2",date2.getText().toString()));
  }
});

In your json activity

if (getIntent().hasExtra("extra1")){
    tvJSON.setText(getIntent().getStringExtra("extra1"));
 }

 if (getIntent().hasExtra("extra2")){
  textView.setText(getIntent().getStringExtra("extra2"));
 }
Praveen
  • 946
  • 6
  • 14
  • when a click the button to open the new activty my app crash and close... – Mohamed A.B Mar 12 '19 at 09:10
  • i have this on main activity: startActivity(new Intent(MainActivity.this, json.class).putExtra("extra1","Entrada: "+dayOfMonth + "/" + (month + 1) + "/" + year).putExtra("extra2","Salida: "+dayOfMonth + "/" + (month + 1) + "/" + year)); secondary activity: TextView tvJSON; TextView tvJSON2; tvJSON.setText(getIntent().getStringExtra("extra1")); tvJSON2.setText(getIntent().getStringExtra("extra2")); – Mohamed A.B Mar 12 '19 at 09:17
  • i try i think i have an error on my code thanks man you helped me a lot. – Mohamed A.B Mar 12 '19 at 09:30
1

Use startActivityForResult when calling your second activity then in your second activity use setResult to bring back selected values to first Activity:

in FirstActivity:

void openSecondActivity(){
    Intent intent = Intent(context, SecondActivity.class)
    startActivityForResult(intent,1004)
    }

@Override
 void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1004) {
            if (resultCode == Activity.RESULT_OK) {
//do something with results
           }
        super.onActivityResult(requestCode, resultCode, data)
    }

In SecondActivity:

void returnResult(String result){
Intent returnIntent = new Intent()
returnIntent.putExtra("result", result)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
tendai
  • 1,172
  • 1
  • 11
  • 22
0

According to documentation.

Send data from FirstActivity:

public void sendMessage(View view) {
    Intent intent = new Intent(this, SecondActivity.class);
    String message = date.getText().toString();
    String message2 = date2.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    intent.putExtra(EXTRA_MESSAGE_2, message2);
    startActivity(intent);
}

Receive data in SecondActivity:

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

    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
    String message2 = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE_2);

    // Capture the layout's TextView and set the string as its text
    TextView textView = findViewById(R.id.textView);
    textView.setText(message);
}
Oleg Golomoz
  • 502
  • 4
  • 12