我想在index
中获得点击行的id
或TableLayout
。一些代码看起来:
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();
// }
// }
}
});
但是这个事件只返回没有。我不知道我的代码中有什么问题。我看不出错误。
任何有用的评论/回答都表示赞赏。
我可以将rows
替换为row
。如果我这样做,那么只有一个 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
会出现一些设计问题。
答案 0 :(得分:0)
我为我的问题找到了最佳解决方案。
我将以下行从 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++;