Mirjalal
Mirjalal

Reputation: 1352

get TableRow id

I want to get index or id of clicked row in TableLayout. Some of the codes looks:

final TableRow rows = new TableRow(this);
rows.setClickable(true);

Click action of the button, which adds rows to 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++;

And in the click event of rows looks:

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();
//                    }
//                }
            }
        });

But this event returns just nothing. I don't know what's the wrong in my codes. I couldn't see mistake.

Any helpful comment/answer appreciated.

P.S I have searched out in the internet about this, but couldn't found the correct solution for this problem. But, maybe this question is possibly duplicate.

Edit

I can replace field rows with row. If I do it there'll be only one TableRow element. So code looks like:

final TableRow row = new TableRow(getApplicationContext());
row.setClickable(true);

Click action of the button, which adds rows to 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++;

In this situation some design problems occur in TableLayout.

Upvotes: 1

Views: 1578

Answers (1)

Mirjalal
Mirjalal

Reputation: 1352

I found the best solution for my problem.

I moved the following lines from inside of onCreate() to button click event:

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++;

Upvotes: 1

Related Questions