How to create a dynamic data table in android?

I looked through tutorials for TableLayouts and other similar things, but it all seems programmed as a static number of rows with a textview. I am wondering if it is possible to create a simple data table with three columns and a variable rowset. I can add / remove elements from Java code.

Basically, there is something like this:

DATA DATA DATA row1 data data row2 data data 

and populate this table with data from an array of objects in the Java activity class. Later, if I want to add another object, it will simply create another line.

For example, if I have this in java:

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

and this is 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 the for loop to add this data to the table in the action, no matter how many rows I need so that everything is in order. If I add a new row4 object, it will just add another row and end with:

 row1 row1 row1 row2 row2 row2 row3 row3 row3 

Hope I wasn’t too vague ... Thanks in advance guys! :)

+6
source share
4 answers

I feel very stupid, but I figured it out myself.

I just created inside myself and dynamically added lines to it through a for loop, which 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); } 

This is a drug war style game ... Try to start the game in the field of game development.

But ... She works and does exactly what I want her to do. Now I can wrap this in a separate method and update it whenever I want. If I want to add a string, I just add an array entry.

I realized that I will answer my question ... lol!

+20
source

If you want to have a fully dynamic table by which you use ListView or RecyclerView , there is a GitHub table view.

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

Since data is managed in a model, you can easily update the table.

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

Since the table provides many options for customization and styling, the result may look like this: (or something else)

enter image description here

Best wishes:)

+1
source

You can also create it using RecyclerView . I think it would be much faster to do. All you have to do is use the GridLayoutManager . the number of network elements in each row is the number of your columns. the title bar can be implemented as another ViewHolder that uses a different ui, and all you need to do with the rest of your data is to put it in one dimensional array.

0
source

You can create any kind of programmatically from Java, for example: TextView tv = new TextView(context);

So you can do this for Table and for your rows and columns.

Once you create a donde, you have to add it to your layout, you need to find the root xml element and then add to it:

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

0
source

Source: https://habr.com/ru/post/951011/


All Articles