我想在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的click事件中,查找:
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();
// }
// }
}
});但是这个事件只返回nothing。我不知道我的密码有什么问题。我看不出错误。
如有任何有帮助的意见或答复,请见谅。
我在网上搜索过这个问题,但没有找到正确的解决办法。但是,也许这个问题是重复的。
编辑
我可以用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中会出现一些设计问题。
发布于 2015-08-14 12:11:31
我找到了解决问题的最好办法。
我将下面的行从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++;https://stackoverflow.com/questions/31982893
复制相似问题