How to switch activity with item specific data sent

Go To StackoverFlow.com

2

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));
    }
}
2012-04-05 17:16
by Tom.Bowen89


1

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
}
2012-04-05 17:34
by JRaymond
No, the only reason I'm using Tableview as it made the most sense to me at the time.

I presume id would be the id of the itme in the list? So when I fill the ListView as long as I set the id for the list item to the database key, then I can get the id out easily - Tom.Bowen89 2012-04-05 18:29

It's even easier than that - just extend CursorAdapter like is described below, and when you pass your cursor from the DB, provided you named your SQL columns the way it likes, it sets the ID property of every view in the listView for you. http://droiddevelop.tumblr.com/post/12322798864/part-136-making-a-custom-cursor-adapte - JRaymond 2012-04-05 20:08


1

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.

2012-04-05 17:32
by Raceimaztion
I've seen this method for passing data. The issue I'm have most problem with is knowing which item was clicked and getting the data specific to that item - Tom.Bowen89 2012-04-05 18:26
I've updated my answer with info about listeners and such - Raceimaztion 2012-04-05 21:29


0

This is a pretty simple procedure. This blog explains it in simple terms.

2012-04-05 17:33
by Jason Robinson
I've seen this method for passing data. The issue I'm have most problem with is knowing which item was clicked and getting the data specific to that item - Tom.Bowen89 2012-04-05 18:27
You can attach data to a view using view.setTag(Object), then retrieve it using view.getTag() - Jason Robinson 2012-04-05 18:40
Ads