Home » Android » java – How to create a dynamic table of data in android?

java – How to create a dynamic table of data in android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’ve looked around at tutorials for TableLayouts and other such things, but all of it seems to be programmed as a static number of rows with textview’s. I’m wondering if it would be possible to create a simple table of data, with 3 columns and a variable set of rows I can add/remove items from in the Java code.

Basically, have something like this:

DATA DATA DATA
row1 data data
row2 data data

and fill this table with data from an object array in the activity’s Java class. Later, if I want to add another object, it will just create another row.

For instance, if I have this in java:

Class data{
    public data(String d1, String d2, String d3){
     data1=d1;
     data2=d2;
     data3=d3;
}
}

and this in the activity class:

Class activity{
data info[] = {
new data("row1", "row1", "row1"), new data("row2","row2","row2"),
new data("row3","row3","row3")};
}
}

And I will use a for loop to add this data into the table in the activity, regardless of how many rows I need for all of it to be fit. If I add a new object of row4, it will just add another row, and end with:

row1 row1 row1
row2 row2 row2
row3 row3 row3

I hope I haven’t been too vague… Thanks in advance, fellas! 🙂

How to&Answers:

I feel very stupid, but I’ve figured this out on my own.

I simply created a inside my , and dynamically added rows to it via a for loop that goes from 0 to myArray.Length().

Bam:

 TableLayout prices = (TableLayout)findViewById(R.id.prices);
    prices.setStretchAllColumns(true);
    prices.bringToFront();
    for(int i = 0; i < drug.length; i++){
        TableRow tr =  new TableRow(this);
        TextView c1 = new TextView(this);
        c1.setText(drug[i].getName());
        TextView c2 = new TextView(this);
        c2.setText(String.valueOf(drug[i].getPrice()));
        TextView c3 = new TextView(this);
        c3.setText(String.valueOf(drug[i].getAmount()));
        tr.addView(c1);
        tr.addView(c2);
        tr.addView(c3);
        prices.addView(tr);
    }

It’s a drug-wars style game… Tryin’ to start small in the game development field.

But… She works, and does exactly what I want it to do. Now I can wrap this into a seperate method and update it whenever I want. If I want to add a row, I just add an array entry.

Figured I’d answer my own question… lol!

Answer:

If you want to have a completely dynamic table as you are used to by ListView or RecyclerView there is the table view on GitHub.

Your code will look like this:

String[][] myData = new String[][] { {"row1", "row1", "row1"},
                                     {"row2", "row2", "row2"},
                                     {"row3", "row3", "row3"} };

TableDataAdapter<String[]> myDataAdapter = 
        new SimpleTableDataAdapter(getContext(), myData);
TableHeaderAdapter myHeaderAdapter = 
        new SimpleTableHeaderAdapter(getContext(), "Header1", "Header2", "Header3");

TableView<String[]> table = findViewById(R.id.table);
table.setDataAdapter(myDataAdapter);
table.setHeaderAdapter(myHeaderAdapter);

Because the data is managed in a model you can update the table very easily.

myDataAdapter.getData().add(new String[]{"row4", "row4", "row4"});
myDataAdapter.getData().add(new String[]{"row5", "row5", "row5"});
myDataAdapter.notifyDatasetChanged();

As the table provides a lot of customization and styling possibilities result could look like this: (or completely different)

enter image description here

Best regards 🙂

Answer:

You can also create it using a RecyclerView. I think it would be a lot quicker to render. All you have to do is to use GridLayoutManager. the number of gridcells on each row is the number of your columns. the Header row can be implemented as a different ViewHolder which uses a different ui and all you have to do with the rest of your data is to put it in a single dimension array.

Answer:

You can create any view programatically from Java, by example: TextView tv = new TextView(context);

So you can do that for the the Table and for their rows and columns.

Once you have donde creating it you have to add it to your layout, you have to find the root xml element and then added to it:

Viewgroup root = findeViewById(R.id.root);
root.addViews(programaticView);