0

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!

Darth Coder
  • 1,738
  • 7
  • 33
  • 56

2 Answers2

1

You can refer to Android online reference: Activity

Starting Activities and Getting Results

The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.

so you should use startActivityForResult. and bind data to intent when sending it back. you can use bundle to attach data.

yushulx
  • 11,695
  • 8
  • 37
  • 64
1

Depending on how many images you have, sharedPreferences could be your bet bet. Based on whatever image the user selects in your Avatar List, give it a number. EX: if you have 10 images, assign each one a number. Use sharedPreferences to import the number selection into your first activity and that will then tell it which one to display through an if statement.

hoss24
  • 556
  • 9
  • 22
  • It seems SharedPreferences is indeed the solution to my problem! Could you please help me with a sample code because I am unable to completely follow the syntax and code examples given on the developer sites and other blogs... – Darth Coder Nov 18 '13 at 04:07