仍在为此寻找答案以查看是否有更好的方法,但是当您创建表格行时,您可以设置标签然后检索它
我有这个与 onClickListener 一起工作:
private void AddRow(String rowNo)
{
final TableLayout table = (TableLayout) findViewById(R.id.my_table);
final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null);
tr.setTag(rowNo);
TextView tv;
tv = (TextView) tr.findViewById(R.id.rowNo);
tv.setText(rowNo);
table.addView(tr);
tr.setOnClickListener(mClickListener);
}
然后在 OnClickListener 中:
private OnClickListener mClickListener = new OnClickListener()
{
public void onClick(View v)
{
try
{
String clickedRow = (String) v.getTag();
Toast toast = Toast.makeText(mContext, clickedRow, Toast.LENGTH_SHORT);
toast.show();
}
catch (Exception ex)
{
Toast toast = Toast.makeText(mContext, ex.toString(), Toast.LENGTH_SHORT);
toast.show();
}
}
};
除非我能找到另一个解决方案,否则我将对 ContextMenu 使用相同的想法
这
- public boolean onContextItemSelected(MenuItem item)
没有视图参数,但是
- public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
做,
所以我想我会使用一个类级别的变量将当前行存储在 onCreateContextMenu 事件中并在 onContextItemSelected 中检索它