I am working on an app and need help with message passing between two screens. The first screen is a Sign-up form with an image button that is supposed to show the user's display pic. When the user first opens this screen, the ImageButton uses a "defaultavatar.png" from the drawable folder. On clicking this imagebutton, the user is directed to a screen with a GridView of icons to choose from. I need to pass the number of the image selected back to the Sign-up activity and change the source of the ImageButton's image accordingly. How do I do this? This is my code currently:
In the Sign-up activity source:
Intent intent = getIntent();
String imagePos = intent.getStringExtra(AvatarListActivity.IMAGE_NUMBER);
//String imageName = "@drawable/bigavatar" + imagePos;
String PACKAGE_NAME = getApplicationContext().getPackageName();
ImageButton displayPic = (ImageButton) findViewById(R.id.displayPic);
int imgId;
if(imagePos != null)
{
imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/bigavatar"+imagePos, null, null);
displayPic.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId)); //src
}
else
{
imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/defaultavatar", null, null);
displayPic.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId)); //src
}
In the Avatar List Activity:
GridView gridview = (GridView) findViewById(R.id.gridViewAvatars);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(AvatarListActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getBaseContext(), SignUpActivity.class);
intent.putExtra(IMAGE_NUMBER, position);
startActivity(intent);
}
});
How do I go about swapping Images depending on the user's choice? (Note: all avatar icons have been numbered as bigavatar1, bigavatar2, bigavatar3 etc.). Please help asap and thanks in advance!