我对 Android 编程相当陌生,并且已经完成了我的第一个应用程序,除了一个关于从选定的表行传递数据的问题 (onClick)。我在这里进行了研究并尝试了大部分建议,但我显然遗漏了一些关键的东西。我对 getTag/Id 和 setTag/Id 有点困惑,所以也许这就是我出错的地方。
一点背景知识,该表可以从数据库动态填充 1 到 N 行,每一行都是可点击的。单击一行后,我需要将该行的唯一数据传递给下一个活动(使用 Bundle 可以正常工作)。不起作用的是,无论我点击哪一行,我只会将最后一行的数据传递给下一个活动,而不是点击的行的数据。
这是我的一些相关代码,用于将数据添加到行/表并单击...我做错了什么只能获取最后一行的数据?非常感谢任何人帮助我指出正确的方向。
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++;
}
};