Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dynamic table of data in android?

Tags:

java

android

xml

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! :)

like image 368
Nicolas Mustaro Avatar asked Aug 05 '13 02:08

Nicolas Mustaro


People also ask

How to create Dynamic table in android?

This example demonstrates how to add table rows Dynamically in Android Layout. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Is table layout available in Android?

Android TableLayout going to be arranged groups of views into rows and columns. You will use the <TableRow> element to build a row in the table. Each row has zero or more cells; each cell can hold one View object. TableLayout containers do not display border lines for their rows, columns, or cells.

What is the table of Android?

Android TableLayout is a ViewGroup subclass which is used to display the child View elements in rows and columns. It will arrange all the children elements into rows and columns and does not display any border lines in between rows, columns or cells.


1 Answers

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 :)

like image 189
Ingo Schwarz Avatar answered Oct 21 '22 01:10

Ingo Schwarz