Sheila Sheila - 24 days ago 7
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) i.next();

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)) {
tvcity.setText("");
} 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.setText("");
tvdummy.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row1.addView(tvdummy);
auctions_table.addView(row1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

// work on the new city data
tvcity.setText(p.getCity());
}

tvcity.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tvcity.setTextColor(getResources().getColor(R.color.red));

//add TextView data to row
row2.addView(tvcity);

// 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))) {
tvdate.setText("");
tvtime.setText("");
} else {
tvdate.setText(p.getDate());
tvtime.setText(p.getTime());
}

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

tvdate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvdate.setTextColor(getResources().getColor(R.color.blue));

tvtime.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvtime.setTextColor(getResources().getColor(R.color.blue));

tvsunit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvsunit.setTextColor(getResources().getColor(R.color.blue));

//add TextView data to row
row3.addView(tvdate);
row3.addView(tvtime);
row3.addView(tvsunit);

row3.setId(t);
row3.setFocusable(false);
//row3.setClickable(true);
row3.setOnClickListener(new View.OnClickListener() {
//public OnClickListener tablerowOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Get the selected row's data
//row3.getId();
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);
StAuctions.putExtras(basket2);
StAuctions.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(StAuctions);
// Closing screen
//finish();
}
});
row3.setBackgroundResource(drawable.list_selector_background);

// 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;
t++;
}
};

Answer

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:

row3.setTag(basket2);