Index of Table Row in TableLayout
I have a TableLayout and some unknown number of TableRows that are generated based on what's in the database.
Every row has an OnClick listener attached, however, once the click happens I can't tell (meaningfully) from what row it came from. Is there a way that I can get the index of the tablerow in relation to the TableLayout or should I try something else?
Some code:
private void buildBudgetTable() {
NumberFormat currencyFormatter = DecimalFormat.getCurrencyInstance();
TableLayout budgetTable = (TableLayout) findViewById(R.id.budgetTable);
budgetTable.removeAllViews();
try {
this.budgetItems = Budget.GetEntries(this);
} catch (SQLException e) {
Toast.makeText(getApplicationContext(), "Could not load budget", Toast.LENGTH_SHORT).show();
e.printStackTrace();
finish();
}
for(BudgetEntryItem entry : budgetItems){
TableRow tr = new TableRow(this);
tr.setPadding(1, 2, 1, 2);
TextView txtLeft = new TextView(this);
txtLeft.setText(entry.Memo);
txtLeft.setGravity(Gravity.LEFT);
开发者_Python百科txtLeft.setPadding(3,3,3,3);
TextView txtRight = new TextView(this);
txtRight.setText(currencyFormatter.format(entry.Amount));
txtRight.setGravity(Gravity.RIGHT);
txtRight.setPadding(3,3,3,3);
if(entry.Amount > 0){
txtRight.setBackgroundColor(Color.rgb(0x33, 0xEE, 0x33));
txtLeft.setBackgroundColor(Color.rgb(0x33, 0xEE, 0x33));
}else{
txtRight.setBackgroundColor(Color.rgb(0xEE, 0x33, 0x33));
txtLeft.setBackgroundColor(Color.rgb(0xEE, 0x33, 0x33));
}
tr.addView(txtLeft);
tr.addView(txtRight);
tr.setOnClickListener(this);
budgetTable.addView(tr);
}
}
Had the same issue, this is the solution:
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Current Row Index
int rowIndex = tableLayout.indexOfChild(v);
// Do what you need to do.
}
});
In my case, I had the Listener on a TextView inside a TableRow...
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// The row the view is in
TableRow row = (TableRow) v.getParent();
// It's index
int index = tableLayout.indexOfChild(row);
}
});
Try this,
row = new TableRow(this);
row.setId(i);
row.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), "Tabele row clicked "+ Integer.toString(v.getId()) , Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
});
Use the TableRow's tag:
BudgetEntryItem entry = null;
for(int i = 0; i < budgetItems; i++){
entry = budgetItems[i]; // or budgetItems.get(i), etc
TableRow tr = new TableRow(this);
// Set the index as the tag
tr.setTag(i); // or entry.setTag(entry.index()), etc
// ...
}
Now to grab the index, just convert it back to an int in your onClick which provides you with the originating View.
An aside
Some people frown on using Tag's in this manner on the principle that it couples the data layer with the UI layer. An alternative would be to use a Dictionary or Dictionary, etc to access the item indirectly without tying the UI element to the data object.
精彩评论