Sheila Sheila - 1 year ago 58
Android Question

Unable to pass correct table row's data to next activity

I'm fairly new to Android programming and have completed my first app except for one issue on passing data from a table row that is selected (onClick). I've researched out here and tried most of the recommendations but I'm obviously missing something critical. I am a little confused on the getTag/Id and setTag/Id so maybe that's where I'm going wrong.

A little background, the table can have 1 to N number of rows populated dynamically from database, each row being clickable. Once a row is clicked, I need to pass the unique data of that row to the next activity (using Bundle for that which works fine). What doesn't work is that no matter which row I click, I only get the last row's data passed to the next activity and not the data of the clicked row.

Here's some of my pertinent code for the data add to row/table and onclick... What am I doing wrong to only get the last row's data? Really appreciate anyone's help in pointing me in the right direction.

public void addData(ArrayList<Auctions> auctions) {

prevCity = "";
currCity = "";
prevDate = "";
currDate = "";
prevTime = "";
currTime = "";
prevUnit = "";
currUnit = "";

for (Iterator i = auctions.iterator(); i.hasNext();) {
Auctions p = (Auctions);

if (p.getCity().equals(prevCity) && p.getDate().equals(prevDate) && p.getTime().equals(prevTime) && p.getSunit().equals(prevUnit)) {
//skip to next unique record
} else {

/** Create a City TableRow dynamically **/
row2 = new TableRow(this);
row2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

/** Creating a TextView to add to the row **/
tvcity = new TextView(this);
currCity = p.getCity();
if (currCity.toString().equals(prevCity)) {
} else {
//create a blank row to separate from previous city entries
row1 = new TableRow(this);
row1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tvdummy = new TextView(this);
tvdummy.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
auctions_table.addView(row1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

// work on the new city data

tvcity.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

//add TextView data to row

// Add the TableRow to the TableLayout
auctions_table.addView(row2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

// Create date, time, and unit name row dynamically
row3 = new TableRow(this);
row3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

// Create TextViews to add to the Row -- Date and Time
tvdate = new TextView(this);
tvtime = new TextView(this);
tvsunit = new TextView(this);

currDate = p.getDate();
currTime = p.getTime();
currUnit = p.getSunit();

if ((currDate.toString().equals(prevDate)) && (currTime.toString().equals(prevTime))) {
} else {

if (currUnit.toString().equals(prevUnit)) {
} else {
tvsunit.setText("Dynamic UNIT");

tvdate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

tvtime.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

tvsunit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

//add TextView data to row

row3.setOnClickListener(new View.OnClickListener() {
//public OnClickListener tablerowOnClickListener = new OnClickListener() {
public void onClick(View v) {
// Get the selected row's data
Bundle basket2 = new Bundle();
basket2.putString("ustate", state.getText().toString());
basket2.putString("abbrev", abbrev);
basket2.putString("ucity", currCity);
basket2.putString("uname", currUnit);
basket2.putString("auctiondate", currDate);
basket2.putString("auctiontime", currTime);

Intent StAuctions = new Intent(getApplicationContext(), StorageAuctionDetails.class);
// Closing screen

// Add the TableRow to the TableLayout
auctions_table.addView(row3, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

prevCity = currCity;
prevDate = currDate;
prevTime = currTime;
prevUnit = currUnit;

Answer Source

You are going to get these variables based on what they are when onClick is activated:

 basket2.putString("ustate", state.getText().toString());
                            basket2.putString("abbrev", abbrev);
                            basket2.putString("ucity", currCity);
                            basket2.putString("uname", currUnit);
                            basket2.putString("auctiondate", currDate);
                            basket2.putString("auctiontime", currTime);

Those valued will be for whatever the last time you ran through your iteration.

You need to create a new "basket" for each row entry. Not in the click listener, but in the iteration loop, and make that basket the tag for that row, with something like this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download