0

I have two activities: First one is MainActivity and the other one is ShowMap. I want to load an image in the 2nd activity based on the spinner value in 1st activity.

To illustrate it in a bit, if a user chooses two places in two spinners and then press a button "Show Map", s/he will able to see an image. If the user chooses another value in spinner, the image should change. How can I achieve it?

MainActivity.java

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner1, spinner2;
ImageView img;
Button button;

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

    // first spinner (From)
    spinner1 = (Spinner) findViewById(R.id.from);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.places, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);

    // second spinner (To)

    spinner2 = (Spinner) findViewById(R.id.to);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
            R.array.places, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter1);


    String value1 = spinner1.getSelectedItem().toString();
    String value2 = spinner2.getSelectedItem().toString();





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

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this,ShowMap.class));


        }
    });


    img = (ImageView) findViewById(R.id.pic_map);
    int image1 = R.drawable.sn;
    int image2 = R.drawable.an;


}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    switch (i){
        case 0:
            img.setBackgroundResource(R.drawable.ac);
            break;
        case 1:
            img.setBackgroundResource(R.drawable.as);
            break;


    }

}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}


}

ShowMap.java

public class ShowMap extends AppCompatActivity {

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


}

1 Answers1

0

First create a value for your images, for example:

String data="img1";

then in your onItemSelected change the value to something like img1 or img2 . Finally in your button.setOnClickListener start activity with intent like:

String value="Hello world";
Intent i = new Intent(CurrentActivity.this, NewActivity.class);    
i.putExtra("key",data);
startActivity(i);

Now in your second activity:

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

and set the image based on the value.

Payam Asefi
  • 2,677
  • 2
  • 15
  • 26
  • Sorry but i can't understand this clearly .. what do you mean of Key ? – Mohamed M. Abo Elmagd Jun 16 '19 at 20:15
  • key is like a secret name between these 2 activities, like posting a box with a password from one place to another. you need the password to open the box. this is what key is. you can use any name you want as a key, just make sure to use the same string in both activities – Payam Asefi Jun 16 '19 at 20:27