首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取TableRow id

获取TableRow id
EN

Stack Overflow用户
提问于 2015-08-13 08:11:50
回答 1查看 1.3K关注 0票数 1

我想在index中得到点击行的idTableLayout。其中一些守则看上去:

代码语言:javascript
复制
final TableRow rows = new TableRow(this);
rows.setClickable(true);

单击按钮的操作,将行添加到TableLayout

代码语言:javascript
复制
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事件中,查找:

代码语言:javascript
复制
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元素。所以代码看起来是:

代码语言:javascript
复制
final TableRow row = new TableRow(getApplicationContext());
row.setClickable(true);

单击按钮的操作,将行添加到TableLayout

代码语言:javascript
复制
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中会出现一些设计问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-14 12:11:31

我找到了解决问题的最好办法。

我将下面的行从onCreate()内部移到按钮,单击事件

代码语言:javascript
复制
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++;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31982893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档