Correct integer not being passed through PutExtra

My program displays a horizontally sliding row of buttons containing the text description of a work of art. When a button is clicked, ImageActivity launches to display the corresponding work of art. When I click on any of the text buttons, though, it always displays the very last painting in the Array.

I'm trying to pass an int ID to a second activity so that it will display the correct painting once it's corresponding description is clicked.

Thank you!

Here's my MainActivity:

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    //CONTAINING PAINTINGS
    private LinearLayout mLinearList;
    private String id;
    private Painting painting;

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

        //REFERENCE THE SCROLLABLE LAYOUT STRUCTURE IN MAIN_SCREEN.XML
        mLinearList = (LinearLayout) findViewById(R.id.linearList);

        //FILL THE SCROLLABLE LAYOUT STRUCTURE WITH PAINTINGS
        fillTextCarousel();

    }

    private void fillTextCarousel() {

        // POPULATE THE LINEAR LIST CAROUSEL WITH PAINTINGS AND DESCRIPTIONS
        Button buttonItem;

        for (int i = 0; i < RenaissanceDatabase.description.length; i++) {
            //STORE THE INDIVIDUAL PAINTINGS AS BUTTONS
            buttonItem = new Button(this);


            painting = new Painting(RenaissanceDatabase.description[i], RenaissanceDatabase.id[i]);

            //USE THE CONTENT DESCRIPTION PROPERTY TO STORE
            //PAINTING DATA

            buttonItem.setContentDescription(painting.getDescription());
            buttonItem.setText(painting.getDescription());

            //SET AN ONCLICK LISTENER FOR THE TEXT BUTTON
            buttonItem.setOnClickListener(displayPainting);

            //ADD THE IMAGE BUTTON TO THE SCROLLABLE LINEAR LIST
            mLinearList.addView(buttonItem);
        }
    }

    private View.OnClickListener displayPainting = new View.OnClickListener() {
         public void onClick(View btn) {
         // COLLECT THE IMAGE STORED FOR THE PAINTING
             //String Painting_ID = Integer.toString(painting.getId());
             Intent imgIntent = new Intent(getApplicationContext(), ImageActivity.class);
             imgIntent.setAction(imgIntent.ACTION_SEND);
             imgIntent.putExtra("image_id", painting.getId());
             startActivity(imgIntent);

         }
    };

My ImageActivity that I am trying to pass an integer ID to:

public class ImageActivity extends Activity {

private Painting painting;
private int index;
private int[] IDs;
private String[] Desc;

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

    Intent objIntent = getIntent();
    int ID_Val = objIntent.getIntExtra("image_id", 0);
    ImageView art = (ImageView) findViewById(R.id.imageView2);
    art.setImageResource(ID_Val);
}

}

And the painting database, which I'm taking taking the ID from:

public class RenaissanceDatabase {

    public static String description[] = {
            "Venus of Urbino\nTitan, 1538",
            "St. John the Baptist\nLeonardo da Vinci, 1516",
            "Protrait of Baldassare Castiglione\nRaphael, 1515",
            "The Entombent of Christ\nCaravaggio, 1603",
            "Coronation of the Virgin\nFra Angelico, 1435",
            "Mars and Venus\n Sandro Bottcelli, 1483"};

    public static int id[] = {
            R.drawable.painting1, // VENUS OF URBINO
            R.drawable.painting2, // ST.JOHN BAPTIST
            R.drawable.painting3, // BALDASSARE
            R.drawable.painting4, // ENTOMBENT OF CHRIST
            R.drawable.painting5, // CORONOATION
            R.drawable.painting6  // MARS AND VENUS
    };
}

Answers


Set the id for every button in your for loop -

for (int i = 0; i < RenaissanceDatabase.description.length; i++) {
            //STORE THE INDIVIDUAL PAINTINGS AS BUTTONS
            buttonItem = new Button(this);


            painting = new Painting(RenaissanceDatabase.description[i], RenaissanceDatabase.id[i]);

            //USE THE CONTENT DESCRIPTION PROPERTY TO STORE
            //PAINTING DATA

            buttonItem.setId(painting.getId());
....
        }
    }

And then change your OnClickListener -

private View.OnClickListener displayPainting = new View.OnClickListener() {
         public void onClick(View btn) {
    ....
             imgIntent.putExtra("image_id", btn.getId());
             startActivity(imgIntent);

         }
    };

The way your code is set up this makes perfect sense.

Look at the loop you're using to configure everything.

for (int i = 0; i < RenaissanceDatabase.description.length; i++) {
.....
    painting = new Painting(RenaissanceDatabase.description[i], RenaissanceDatabase.id[i]);
...
}

What is the value of painting at the end of this loop? It's whatever was in the last position in the array.

Now look at your code to pass the ID:

private View.OnClickListener displayPainting = new View.OnClickListener() {
     public void onClick(View btn) {
         ....
         imgIntent.putExtra("image_id", painting.getId());
         .....
     }
};

The code presented never updates the "current painting" to the one which was touched. You need to figure out which painting the user has touched, and use the ID of that painting.

Another answer has a recommendation to set the buttonId to the id of the painting - this has some issues so I wouldn't personally recommend that.

Instead I would use an Adapter and ViewHolder pattern and leverage the viewholder to contain this meta data. This is a much more scalable solution.


Need Your Help

Unable to access resources on linux in windows domain

mysql linux windows networking

I am trying to access MySQL on a linux box from a windows server. I have created a user foo@10.% and commented out the bind-address line in my.cnf and I am still unable to connect.

Modal window sharepoint 2010 set focus on button control

sharepoint focus tabindex modal-window

I am trying to set focus on a button of a modal window(that is opening a new page). The problem i am facing is if am using only the keyboard to navigate in the web page it is stopping at the maximize