java TableLayout中表格行的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6976971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Index of Table Row in TableLayout
提问by Malfist
I have a TableLayout and some unknown number of TableRows that are generated based on what's in the database.
我有一个 TableLayout 和一些未知数量的 TableRow,它们是根据数据库中的内容生成的。
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?
每一行都有一个 OnClick 侦听器,但是,一旦点击发生,我就无法(有意义地)告诉它来自哪一行。有没有办法可以获取与 TableLayout 相关的 tablerow 的索引,还是应该尝试其他方法?
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);
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);
}
}
采纳答案by Austin Hanson
Use the TableRow's tag:
使用 TableRow 的标签:
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.
现在要获取索引,只需在您的 onClick 中将其转换回一个 int,它为您提供原始视图。
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.
旁白
有些人不赞成以这种方式使用Tag,因为它会将数据层与UI 层耦合在一起。另一种方法是使用 Dictionary 或 Dictionary 等间接访问项目,而无需将 UI 元素绑定到数据对象。
回答by Ullauri
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...
就我而言,我在 TableRow 内的 TextView 上有监听器...
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);
}
});
回答by Thowfeek
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();
}
});