Android - Problemas al ingresar datos en un tablelayout

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

Problemas al ingresar datos en un tablelayout

Publicado por Hector (35 intervenciones) el 16/08/2021 18:16:51
Hola amigos del foro esperando que todos se encuentren muy bien de salud, les comento que estoy aprendiendo a ingresar datos en una table layout, el cual ingreso mi primea fila, ero al ingresar la segunda fila no lo realiza tal procedimiento, les comento además que soy nuevo en estas lides de android, favor de asesorarme en que estoy fallando, agredo el código de mi clase que utilizo:
import android.content.Context;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.ArrayList;

public class TableDynamic {
private TableLayout tableLayout;
private Context context;
private String[] header;
private ArrayList<String[]> data;
private TableRow tableRow;
private TextView txtCell;
private int indexC;
private int indexR;
private boolean multiColor = false;
int firtColor, secondColor, textColor, colorLinea;

public TableDynamic(TableLayout tableLayout, Context context) {
this.tableLayout = tableLayout;
this.context = context;
}

public void addHeader(String[] header) {
this.header = header;
createHeader();
}

public void addData(ArrayList<String[]> data) {
this.data = data;
createDataTable();
}

private void newRow() {
tableRow = new TableRow(context);
}

private void newCell() {
txtCell = new TextView(context);
txtCell.setGravity(Gravity.CENTER);
txtCell.setTextSize(18);
}

private void createHeader() {
indexC = 0;
newRow();
while (indexC < header.length) {
newCell();
txtCell.setText(header[indexC++]);
tableRow.addView(txtCell, newTableRowParams());
}
tableLayout.addView(tableRow);
}

private void createDataTable() {
String info;
for(indexR=1;indexR<= data.size();indexR++) {
newRow();
for(indexC=0;indexC<header.length;indexC++) {
newCell();
String[] row = data.get(indexR-1);
info = (indexC<row.length)?row[indexC]:"";
txtCell.setText(info);
tableRow.addView(txtCell,newTableRowParams());
}
tableLayout.addView(tableRow);
}
}

public void addItems(String[] item) {
String info;
data.add(item);
indexC = 0;
newRow();
while (indexC < header.length) {
newCell();
info = (indexC < item.length) ? item[indexC++] : "";
txtCell.setText(info);
tableRow.addView(txtCell, newTableRowParams());
}
tableLayout.addView(tableRow, data.size());//Se quito el -1 despues de size para corregir
reColoring();
reColoringLinea();
}

public void backgroundHeader(int color) {
indexC = 0;
//newRow();
while (indexC < header.length) {
txtCell = getCell(0, indexC++);
txtCell.setBackgroundColor(color);
}
}

public void backgroundData(int firtColor, int secondColor) {
for (indexR = 1; indexR <= data.size(); indexR++) {
multiColor = !multiColor;
for (indexC = 0; indexC < header.length; indexC++) {
txtCell = getCell(indexR, indexC);
txtCell.setBackgroundColor((multiColor) ? firtColor : secondColor);
}
}
this.firtColor = firtColor;
this.secondColor = secondColor;
}

public void lineColor(int color) {
indexR = 0;
while (indexR <= data.size()) {
getRow(indexR++).setBackgroundColor(color);
}
this.colorLinea = color;
}

public void textColorData(int color) {
for (indexR = 1; indexR <= data.size(); indexR++) {
for (indexC = 0; indexC < header.length; indexC++) {
getCell(indexR, indexC).setTextColor(color);
}
}
this.textColor = color;
}

public void textColorHeader(int color) {
indexC = 0;
while (indexC < header.length) {
getCell(0, indexC++).setTextColor(color);
}
}

public void reColoring() {
indexC = 0;
multiColor=!multiColor;
while (indexC < header.length) {
txtCell = getCell(data.size(), indexC++);
txtCell.setBackgroundColor((multiColor) ? firtColor : secondColor);
txtCell.setTextColor(textColor);
}
}

public void reColoringLinea(){
indexR = 0;
while (indexR <= data.size()) {
getRow(indexR++).setBackgroundColor(colorLinea);
}
}

private TableRow getRow(int index) {
return (TableRow) tableLayout.getChildAt(index);
}

private TextView getCell(int rowIndex, int columIndex) {
tableRow = getRow(rowIndex);
return (TextView) tableRow.getChildAt(columIndex);
}

private TableRow.LayoutParams newTableRowParams() {
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.setMargins(1, 1, 1, 1);
params.weight = 1;
return params;
}

y este es el código en el mainactivity

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

EditText et_filas, et_tipo, et_marca, et_modelo;
Button ingresar, modificar;
TableLayout tbldatos;
private String[]header={"N° Filas","Tipo","Marca","Modelo"};
private ArrayList<String[]> rows=new ArrayList<>();
private TableDynamic tableDynamic;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_filas = findViewById(R.id.txtitem);
et_tipo = findViewById(R.id.txttipo);
et_marca = findViewById(R.id.txtmarca);
et_modelo = findViewById(R.id.txtmodelo);
ingresar = findViewById(R.id.cmdingresar);
modificar = findViewById(R.id.cmdmodificar);
tbldatos = findViewById(R.id.tbldatos);

tableDynamic = new TableDynamic(tbldatos, getApplicationContext());
tableDynamic.addHeader(header);
tableDynamic.addData(getClients());
tableDynamic.backgroundHeader(Color.BLUE);
tableDynamic.backgroundData(Color.RED, Color.YELLOW);
tableDynamic.lineColor(Color.BLACK);
tableDynamic.textColorData(Color.WHITE);
tableDynamic.textColorHeader(Color.MAGENTA);
}

private ArrayList<String[]> getClients(){

return rows;
}

public void datos (View view){
String[]item = new String[]{et_filas.getText().toString(),et_tipo.getText().toString(),et_marca.getText().toString(),et_modelo.getText().toString()};
tableDynamic.addItems(item);
}

ojala que me puedan ayudar en que estoy fallando, que no me agrega la segunda fila, ya que quiero aprender dicha herramienta, y desde ya muchas gracias.
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
sin imagen de perfil
Val: 40
Ha aumentado su posición en 2 puestos en Android (en relación al último mes)
Gráfica de Android

Problemas al ingresar datos en un tablelayout

Publicado por Hector (35 intervenciones) el 19/08/2021 23:34:41
Amigos del foro muchas gracias por su ayuda, después de un exhaustivo análisis, me di cuenta del error, me di cuenta que soy un imbécil HDP, pero me gusta compartir mis dudas y arreglos, en la clase:
public void backgroundHeader(int color) {
indexC = 0;
//newRow();
while (indexC < header.length) {
txtCell = getCell(0, indexC++);
txtCell.setBackgroundColor(color);
}
}
si se dan cuenta lo tengo en comentario el newRow();, por lo cual quitan el comentario y funciona sin ningún problema, y disculpen las molestias que pude haber generado y nuevamente muchas gracias por su ayuda, no me gusta ser irónico pero no se porque a la fecha del día de hoy todas mis dudas ue tengo en android studio, debido a que soy novato en estas lides no me han respondido, no se porque si nunca he sido groseros con nadie.
Pero no se preocupen voy a volver a molestar con otro problema que poseo.
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