I am trying to modify an image of an ImageView in one activity using a spinner, and then send that image to an ImageButton in another activity.
Here is the code of the ImageView activity:
imgView = (ImageView) findViewById(R.id.imgView);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent imgIntent = getIntent();
switch (position) {
case 0:
imgView.setImageResource(R.drawable.brown);
imgIntent.putExtra("image", R.drawable.brown);
break;
case 1:
imgView.setImageResource(R.drawable.blue);
imgIntent.putExtra("image", R.drawable.blue);
break;
case 2:
imgView.setImageResource(R.drawable.black);
imgIntent.putExtra("image", R.drawable.black);
break;
case 3:
imgView.setImageResource(R.drawable.white);
imgIntent.putExtra("image", R.drawable.white);
break;
default:
imgView.setImageResource(R.drawable.brown);
imgIntent.putExtra("image", R.drawable.brown);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Here is the code of the ImageButton activity:
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
Intent imgIntent = getIntent();
imgBtn.setImageResource(imgIntent.getIntExtra("image"));
I cannot figure out how to do this.
Full code of MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
Intent imgIntent = getIntent();
imgBtn.setImageResource(imgIntent.getIntExtra("image", 0));
startActivity(imgIntent);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CustomizeDoggo.class);
startActivity(intent);
}
});
}
}
just change this line.You haven't passed the default value
imgBtn.setImageResource(imgIntent.getIntExtra("image",0));
and u haven't started the activity
startActivity(mIntent);
UPDATE
Start an intent like this
//This line is missing in your code
Intent intent = new Intent(CurrentActivity.this,ActivityToOpen.class);
intent.putExtra("image",id of selected image);
startActivity(intent);
在dest Activity中:在onCreate()
方法中调用getArgurment()
以获取包,然后在getIntExtra("image",default value);
wait try this will surely work
In main activity while starting new activity use this code instead of yours
Intent=new Intent(mContext,SecondActivity.class);
intent.putExtra("image",R.drawable.blue);
startActivity(intent);
now in second activity in oncreate use this code
int image=getIntent().getExtras().getInt("image",0);
mPlayPauseImageButton.setImageResource(image);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.