Ronie86
Ronie86

Reputation: 3

How to know in which TableRow a CheckBox is placed

I have a TableLayout with TableRows and CheckBox and TextView inside. When I push a CheckBox I need to know in wich TableRow the CheckBox is placed to get the TextView "associated" to this CheckBox. For example:

TableLayout:

I'm trying something like this:

cb.setOnClickListener(new View.OnClickListener() {                                              
    @Override
    public void onClick(View v) {
    if (cb.isChecked())
    {
        int id_cb = cb.getId();
        boolean encontrado = false;
        for (int i = 0; i < tabla_tareas.getChildCount() && !encontrado; i++)
        {
            TableRow aux_tr = (TableRow) tabla_tareas.getChildAt(i);
            CheckBox aux_cb = (CheckBox) aux_tr.getChildAt(0);
            if (id_cb == aux_cb.getId())
                   encontrado = true;
        }...

But it doesn't work.

Upvotes: 0

Views: 530

Answers (1)

TronicZomB
TronicZomB

Reputation: 8747

Utilizing what Elior has said, why don't you just use the folling methods to achieve what you want:

cb.setOnClickListener(new View.OnClickListener() {                                              
@Override
public void onClick(View v) {
if (cb.isChecked())
{
    cb.getText().toString(); //This will retrieve the text associated with the Checkbox
    cb.setText("Your new string here"); //This will set the text associated with the CheckBox
    }...

With the above two methods you should be able to do everything you need to without the need for the TextView, and probably without the TableLayout as well since I am assuming you were using that to align the CheckBoxes and the TextViews.

Upvotes: 1

Related Questions