How to switch activity with item specific data sent

I've had a look around, and it might be because I'm not sure what I'm looking for, but I can't find out how to do something I presume should be quite easy with android.

I have an array of data to display on the screen. This data is a class that holds a database key, name and image.

I'm currently displaying this data as an ImageView and a TextView. I loop through the array and add a new row to a TableLayout containing the image and text.

I'd like both the image and text to be clickable, changing to a new activity.

This new activity needs to know the database key of the row clicked in order to display the correct data.

Here's what I have so far:

private void fillSuggestionTable(TableLayout tabSuggestions, Suggestion[] arrToAdd)
{
    for(int i = 0; i < arrToAdd.length; i++)
    {
        /* Create a new row to be added. */
        TableRow trSuggestion = new TableRow(this);
        trSuggestion.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        /* Create objects for the row-content. */
        ImageView imgDisplayPicture = new ImageView(this);
        ImageHandler.loadBitmap(arrToAdd[i].strImageURL, imgDisplayPicture);
        imgDisplayPicture.setLayoutParams(new LayoutParams(50,50));

        TextView txtArtistName = new TextView(this);
        txtArtistName.setText(arrToAdd[i].strName);
        txtArtistName.setTextColor(Color.parseColor("#000000"));

        /* Add data to row. */
        trSuggestion.addView(imgDisplayPicture);
        trSuggestion.addView(txtArtistName);

        /* Add row to TableLayout. */
        tabSuggestions.addView(trSuggestion, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    }
}

Answers


Is there a reason you're using a TableView? it seems like what you want to accomplish would be much easier with a ListView & custom CursorAdapter, where the adapter can handle translating from the database to the ListView row. At that point starting a new activity that knows the database ID is trivial:

mListView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
    Intent i = new Intent(MyActivity.this, MyOtherActivity.class);
    i.putExtra("database_id", id);
    startActivity(i);
  }
});

And in MyOtherActivity:

private int dbId;
protected void onCreate(Bundle savedInstanceState) {
  //do stuff
  dbId = getIntent().getIntExtra("database_id", -1); // the -1 is the default if the extra can't be found
}

To pass extra data to another Activity, you need to add extra information with the Intent.putExtra(name, value) methods.

For example, to send the Intent:

Intent i = new Intent([pass info about next Activity here]);
i.putExtra("databaseKey", databaseKey);
startActivity(i);

To get the data out again:

public void onCreate(Bundle savedInstance)
{
    // Do all initial setup here

    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey("databaseKey"))
    {
        int databaseKey = extras.getInt("databaseKey");
        // Load database info
    }
    else
    {
        // No data was passed, do something else
    }
}

EDIT: To find out when the table's row is clicked, you'll need to implement View.OnClickListener and set the onClickListener for the TableRows you use.

For example:

/* Create a new row to be added. */
TableRow trSuggestion = new TableRow(this);
trSuggestion.setOnClickListener([listener]);

The only problem you'll have is relating a View's ID to the related database row ID. A HashMap should help.


Need Your Help

"HttpContext.Current.Session" vs Global.asax "this.Session"

asp.net session session-state global-asax

Recently, while working on some code for an ASP.NET project at work. We needed a tracking util to take basic metrics on user activity (page hit count etc) we would track them in Session, then save ...

Bootstrap : breadcrumb remove slash?

html5 css3 twitter-bootstrap twitter-bootstrap-3 breadcrumbs

In Bootstrap 3 there is default slash(/) symbol between breadcrumb elements. How to remove it? And I am want to replace '/' with '>'.