Send data from one Activity to another in Android Studio [duplicated]

0

I have a layout that contains a gridview and another in which it contains a textview and a imageview , in one it captures images and another text, by clicking on the image what I want the text to travel to another layout.

This is my Main Activity:

public class MainActivity extends AppCompatActivity {

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

        final String[] web = {"Android", "Historia",
                "Matematica"};

        int[] imageId = {R.drawable.blue,
                R.drawable.green, R.drawable.red,};

        final ImageAdapter adapter = new ImageAdapter(MainActivity.this, web, imageId);
        GridView grid = (GridView) findViewById(R.id.gridView);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

                //TextView textView = findViewById(R.id.grid_text);

                Intent intent = new Intent(MainActivity.this, MainActivity2.class);

                intent.putExtra("curso", adapter.getItem(position).toString());

                startActivity(intent);

            }
        });
    }
}

It seems that the putExtra is the problem.

    
asked by Jaime Leon Melendez 21.09.2018 в 18:24
source

2 answers

0

To send a value to another Activity use Extras in the Intent:

String valor = "hola mundo"

Intent intent = new Intent(this, OtraActivity.class);
intent.putExtra("valor", valor);
startActivity(intent);

To receive the value in the other Activity is:

Intent intent = this.getIntent();
Bundle extra = intent.getExtras();

String miValor = extra.getString("valor");

To receive the value, make sure that:

  • The name of the received value is the same as the one sent.
  • The type of data received is the same as we sent. (for example: If you send a String uses .getString () to receive it, if int is used getInt (), etc.)
answered by 21.09.2018 в 20:01
-1

If you want to send the posicion of an item from GridView to another activity , one way is precisely with Intent putExtra , like this:

   // de esta forma lo envías como String
intent.putExtra("curso", Integer.toString(position));

   // de esta otra, lo envías como int
intent.putExtra("curso2", position);

In the Activity receiver you receive it with Bundle , within onCreate :

      // inicializas el String y el int de esta forma:
    String value1 = "";
    int value2 = 0;

      // luego obtienes los valores que le envió la otra activity
      // para String declaras default "", y para int un 0
    Bundle extras = getIntent().getExtras();
    if (extras !=null){
        value1 = extras.getString("curso", "");
        value2 = extras.getInt("curso2",0);
    }
      // configuras los valores
    textView.setText(value1);
    textView2.setText(Integer.toString(value2));
    
answered by 21.09.2018 в 23:04