Android Studio - Get value from an EditText from another Activity

0

I have a problem, when I want to reference in my main activity a value of an object of another activity, it returns null even though I used inflater. This is my code:

       LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View view = inflater.inflate(R.layout.list_item ,null);
        final EditText et1 = (EditText)view.findViewById(R.id.et1);
 String valor = et1.getText().toString();

When I show the value variable, it returns null even though I enter the data in the emulator.

    
asked by EdU17 07.10.2017 в 00:12
source

1 answer

-1

You are defining the value of the variable valor when the UI is built, at this point the value is null,

 final View view = inflater.inflate(R.layout.list_item ,null);
 final EditText et1 = (EditText)view.findViewById(R.id.et1);
 String valor = et1.getText().toString();

You can not really get the EditText value defined in another Activity by getting the reference by findViewById(R.id.et1) , for that you can send the value of your EditText via a bundle.

If you want to send the value of a EditText from another Activity, do it using the Intent:

Passing data between activities

for example:

Intent intent = new Intent(;MainActivity.this, SegundaActivity.class);
intent.putExtra("valor_edittext", editext.getText().ToString());
startActivity(intent);

when you receive it in your Second Activity, simply read the value in this way:

  //Obtiene valor!
  String valorRecibido = bundle.getStringExtra("valor_edittext");

  final View view = inflater.inflate(R.layout.list_item ,null);
  final EditText et1 = (EditText)view.findViewById(R.id.et1);

  et1.setText(valorRecibido); //asigna valor a EditText.
    
answered by 07.10.2017 в 00:37