获取表格行 ID
get TableRow id
我想获取 TableLayout
中单击行的 index
或 id
。一些代码看起来:
final TableRow rows = new TableRow(this);
rows.setClickable(true);
按钮的单击操作,向 TableLayout 添加行
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setTag(String.valueOf(queue_number));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row);
queue_number++;
并且在 rows
的点击事件中看起来:
rows.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), String.valueOf(v.getId()), LENGTH_LONG).show();
// client_name.setText(String.valueOf(rows.getChildAt(1)));
// TableRow t = (TableRow) v.getParent();
// Toast.makeText(getApplicationContext(), String.valueOf(t.getId()), LENGTH_LONG).show();
// for (int id = 0; id < root_table.getChildCount(); id++) {
// if (v.getId() == id) {
// Toast.makeText(Satish.this, String.valueOf(r[0].getId()), LENGTH_LONG).show();
// }
// }
}
});
但是这个事件returns只是没什么。我不知道我的代码有什么问题。我看不出错误。
任何有帮助的comment/answer表示赞赏。
P.S 我已经在网上搜索过这个问题,但没有找到解决这个问题的正确方法。但是,也许这个问题可能是重复的。
编辑
我可以用 row
替换字段 rows
。如果我这样做,将只有 一个 TableRow
元素。所以代码看起来像:
final TableRow row = new TableRow(getApplicationContext());
row.setClickable(true);
按钮的单击操作,向 TableLayout 添加行
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setTag(String.valueOf(queue_number));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row);
queue_number++;
在这种情况下,TableLayout
中出现了一些设计问题。
我找到了解决问题的最佳方法。
我将以下行从 内部 onCreate()
移动到 按钮点击事件 :
final TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setId(Integer.valueOf(String.valueOf(queue_number)));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row/*, queue_number*/);
// set onClickListener at runtime. This is working well for me.
root_table.getChildAt(queue_number).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TableRow tr = (TableRow) v;
TextView num, p_n, u_o_m, p, s;
// num = (TextView) tr.getChildAt(0);
p_n = (TextView) tr.getChildAt(1);
u_o_m = (TextView) tr.getChildAt(2);
p = (TextView) tr.getChildAt(3);
s = (TextView) tr.getChildAt(4);
// row_id = Integer.getInteger(num.getText().toString());
product_name.setText(p_n.getText().toString());
unit_of_measurement.setText(u_o_m.getText().toString());
price.setText(p.getText().toString());
sum.setText(s.getText().toString());
}
});
queue_number++;
我想获取 TableLayout
中单击行的 index
或 id
。一些代码看起来:
final TableRow rows = new TableRow(this);
rows.setClickable(true);
按钮的单击操作,向 TableLayout 添加行
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setTag(String.valueOf(queue_number));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row);
queue_number++;
并且在 rows
的点击事件中看起来:
rows.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), String.valueOf(v.getId()), LENGTH_LONG).show();
// client_name.setText(String.valueOf(rows.getChildAt(1)));
// TableRow t = (TableRow) v.getParent();
// Toast.makeText(getApplicationContext(), String.valueOf(t.getId()), LENGTH_LONG).show();
// for (int id = 0; id < root_table.getChildCount(); id++) {
// if (v.getId() == id) {
// Toast.makeText(Satish.this, String.valueOf(r[0].getId()), LENGTH_LONG).show();
// }
// }
}
});
但是这个事件returns只是没什么。我不知道我的代码有什么问题。我看不出错误。
任何有帮助的comment/answer表示赞赏。
P.S 我已经在网上搜索过这个问题,但没有找到解决这个问题的正确方法。但是,也许这个问题可能是重复的。
编辑
我可以用 row
替换字段 rows
。如果我这样做,将只有 一个 TableRow
元素。所以代码看起来像:
final TableRow row = new TableRow(getApplicationContext());
row.setClickable(true);
按钮的单击操作,向 TableLayout 添加行
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setTag(String.valueOf(queue_number));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row);
queue_number++;
在这种情况下,TableLayout
中出现了一些设计问题。
我找到了解决问题的最佳方法。
我将以下行从 内部 onCreate()
移动到 按钮点击事件 :
final TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setId(Integer.valueOf(String.valueOf(queue_number)));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row/*, queue_number*/);
// set onClickListener at runtime. This is working well for me.
root_table.getChildAt(queue_number).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TableRow tr = (TableRow) v;
TextView num, p_n, u_o_m, p, s;
// num = (TextView) tr.getChildAt(0);
p_n = (TextView) tr.getChildAt(1);
u_o_m = (TextView) tr.getChildAt(2);
p = (TextView) tr.getChildAt(3);
s = (TextView) tr.getChildAt(4);
// row_id = Integer.getInteger(num.getText().toString());
product_name.setText(p_n.getText().toString());
unit_of_measurement.setText(u_o_m.getText().toString());
price.setText(p.getText().toString());
sum.setText(s.getText().toString());
}
});
queue_number++;