Android - Recorrer filas de un TableLayout

 
Vista:
sin imagen de perfil
Val: 14
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

Recorrer filas de un TableLayout

Publicado por bladimir (9 intervenciones) el 25/09/2017 04:50:04
Hola a todos. Tengo un TableLayout que se llena en forma dinamica a partir de una base de datos. Hasta aqui todo bien. Cada fila tiene 6 columnas de las cuales la primera es un checkbox y las siguientes son textView. He creado un codigo para recorrer las filas de la tabla. Y funciona porque al colocar el numero de filas en en un Toast este funciona. El problema se presenta a la hora de seleccionar los checkbox marcados. Necesito obtener el texto de esos checkbox pero el codigo no me funciona. El codigo es el siguiente:
1
2
3
4
5
6
7
8
TableLayout tabla = (TableLayout)this.findViewById(R.id.tableLayout);
String nume =  null;
for (int i = 1; i < tabla.getChildCount(); i++) {
    TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.fila1, null);
    CheckBox check1 = (CheckBox) row.findViewById(R.id.check1);
    if (check1.isChecked()) nume = check1.getText().toString();
}
Toast.makeText(getApplicationContext(), nume, Toast.LENGTH_LONG).show();

Mucho agradeceria cualquier ayuda.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Francisco
Val: 466
Oro
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

Recorrer filas de un TableLayout

Publicado por Francisco (358 intervenciones) el 25/09/2017 09:20:19
1
2
3
4
5
6
7
8
9
TableLayout tabla = (TableLayout)this.findViewById(R.id.tableLayout);
String nume = null;
for (int i = 1; i < tabla.getChildCount(); i++) {
TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.fila1, null);
CheckBox check1 = (CheckBox) row.findViewById(R.id.check1);
if (check1.isChecked()) nume = check1.getText().toString();
}
 
Toast.makeText(getApplicationContext(), nume, Toast.LENGTH_LONG).show();


Segun el codigo que adjuntas tienes un tablelayout en el archivo.xml y un linear layout dentro de este.

Luego inflas un tableRow dentro del layout.



Los check no estan dentro ni de tablelayout,ni del tablerow tampoco estan dentro los check

por eso al recorrer el table layout no puede encontrar los check


Si copias y pegas el archivo.xml podremos ver como tienes los wiew incorporados

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 14
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

Recorrer filas de un TableLayout

Publicado por bladimir (9 intervenciones) el 25/09/2017 11:05:19
Hola Francisco. Gracias por responder. El archivo xml donde esta el TableLayout se llama prueba.xml y es este:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TableLayout
        android:id="@+id/tableLayout1"
        android:background="#000080"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/titulotb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text=""
            />
        <TableRow>
            <Button
                android:gravity="center"
                android:layout_gravity="center"
                android:id="@+id/btn1"
                android:layout_width="200.0px"
                android:layout_height="wrap_content"
                android:layout_marginTop="20.0px"
                android:text="Desde:" />
            <EditText
                android:id="@+id/desde"
                android:gravity="center"
                android:layout_gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=""
                />
        </TableRow>
        <TableRow>
            <Button
                android:gravity="center"
                android:layout_gravity="center"
                android:id="@+id/btn2"
                android:layout_width="200.0px"
                android:layout_height="wrap_content"
                android:layout_marginTop="20.0px"
                android:text="Hasta:" />
            <EditText
                android:id="@+id/hasta"
                android:gravity="center"
                android:layout_gravity="center"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=""
                />
        </TableRow>
        <TableRow>
            <Button
                android:gravity="center"
                android:layout_gravity="center"
                android:id="@+id/btn3"
                android:layout_width="200.0px"
                android:layout_height="wrap_content"
                android:layout_marginTop="20.0px"
                android:text="Ver:" />
 
        </TableRow>
    </TableLayout>
 
 
    <ScrollView
        android:id="@+id/layout"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:scrollbars="horizontal|vertical"
        android:layout_width="fill_parent"
        android:layout_marginTop="5dip"
        android:scrollbarStyle="outsideInset"
        android:fillViewport="true">
 
        <HorizontalScrollView
            android:id="@+id/horizontalView"
            android:layout_height="fill_parent"
            android:scrollbars="horizontal|vertical"
            android:layout_width="wrap_content"
            android:layout_marginTop="5dip">
            <TableLayout
                android:id="@+id/tableLayout"
                android:background="#000080"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
 
            </TableLayout>
 
        </HorizontalScrollView>
    </ScrollView>
</LinearLayout>

Por su parte las filas se forman a partir de este archivo layout llamado fila1.xml y su codigo es este:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
<TableRow  android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBox
        android:id="@+id/check1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginRight="10dp"
        android:textColor="@android:color/black" />
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#F05300"
        android:background="@drawable/tabla_celda"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#F05300"
        android:background="@drawable/tabla_celda"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#F05300"
        android:background="@drawable/tabla_celda"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#F05300"
        android:background="@drawable/tabla_celda"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:textColor="#F05300"
        android:layout_height="fill_parent"
        android:background="@drawable/tabla_celda"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#F05300"
        android:textAppearance="@style/estilo_celda"
        android:background="@drawable/tabla_celda"
        />
</TableRow>
Gracias de antemano.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Francisco
Val: 466
Oro
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

Recorrer filas de un TableLayout

Publicado por Francisco (358 intervenciones) el 25/09/2017 15:13:05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
String texto = "";
 
 
TableLayout tabla = (TableLayout) this.findViewById(R.id.tableLayout);
String nume = null;
 
 
 
TableLayout contenedor = (TableLayout) findViewById(R.id.tableLayout);
 
for (int r=0;r<5;r++){  // Aqui debes de recorrer los datos de la base de datos y en cada fila 
    //se cargar una fila1
    LayoutInflater inflater = LayoutInflater.from(this);
    View laViewInflada = inflater.inflate(R.layout.fila1, contenedor, true);
}
 
 
 
 
 
// Una ves inflado el layout y cargado con sus datos puedes recorrer todos los elemrntos de tablelayout
 
for (int i2 = 0; i2 < contenedor.getChildCount(); i2 ++) {
    TableRow v = (TableRow) contenedor.getChildAt(i2);
 
    v.setBackgroundColor(Color.RED);
 
    for (int i3 = 1; i3 < v.getChildCount(); i3 ++) {
 
       View v2 = (View) v.getChildAt(i2);
 
        if( v2 instanceof CheckBox) {
            if (((CheckBox) v2).isChecked()) {
                texto = ((CheckBox) v2).getText().toString();
            }
        }
 
    }
 
}
 
Toast.makeText(getApplicationContext(), texto, Toast.LENGTH_LONG).show();
//        }
}


Espero que te halla esuelto el problemar

saludois..
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 14
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

Recorrer filas de un TableLayout

Publicado por bladimir (9 intervenciones) el 25/09/2017 20:14:07
Apliquè el codigo que me propusiste haciendo las respetivas adaptaciones pero aparece el Toast sin el texto del checkbox. El codigo quedo asi:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
TableLayout tabla = (TableLayout) findViewById(R.id.tableLayout);
for (int r=0;r<7;r++){  // Aqui debes de recorrer los datos de la base de datos y en cada fila
    //se cargar una fila1
    LayoutInflater inflater = LayoutInflater.from(this);
    View laViewInflada = inflater.inflate(R.layout.fila1, tabla, true);
}
for (int i2 = 0; i2 < tabla.getChildCount(); i2 ++) {
    TableRow v = (TableRow) tabla.getChildAt(i2);
    for (int i3 = 1; i3 < v.getChildCount(); i3 ++) {
        View v2 = (View) v.getChildAt(i2);
        if( v2 instanceof CheckBox) {
            if (((CheckBox) v2).isChecked()) {
                texto = ((CheckBox) v2).getText().toString();
 
            }
        }
    }
}
Toast.makeText(getApplicationContext(), texto, Toast.LENGTH_LONG).show();
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Francisco
Val: 466
Oro
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

Recorrer filas de un TableLayout

Publicado por Francisco (358 intervenciones) el 25/09/2017 20:21:24
Si el checkBox .isCheckedd()=false el texto es nulo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 14
Ha mantenido su posición en Android (en relación al último mes)
Gráfica de Android

Recorrer filas de un TableLayout

Publicado por bladimir (9 intervenciones) el 25/09/2017 21:37:30
Marco el checkBox y aun asi no aparece el texto. El toast aparece vacio.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar