Java 如何在android中创建动态数据表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18049708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a dynamic table of data in android?
提问by Nicolas Mustaro
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.
我已经环顾了 TableLayouts 和其他类似内容的教程,但所有这些似乎都被编程为带有 textview 的静态行数。我想知道是否可以创建一个简单的数据表,其中包含 3 列和一组可变的行,我可以在 Java 代码中添加/删除项目。
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.
并用活动的 Java 类中的对象数组中的数据填充此表。稍后,如果我想添加另一个对象,它只会创建另一行。
For instance, if I have this in java:
例如,如果我在 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:
我将使用 for 循环将此数据添加到活动的表中,无论我需要多少行才能适合所有数据。如果我添加一个新的 row4 对象,它只会添加另一行,并以:
row1 row1 row1
row2 row2 row2
row3 row3 row3
I hope I haven't been too vague... Thanks in advance, fellas! :)
我希望我没有太含糊......提前致谢,伙计们!:)
回答by Nicolas Mustaro
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().
我只是在 my 内部创建了一个,并通过一个从 0 到 myArray.Length() 的 for 循环动态地向其中添加了行。
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!
想我会回答我自己的问题...大声笑!
回答by Hossein Shahdoost
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.
您也可以使用RecyclerView. 我认为渲染会快很多。您所要做的就是使用GridLayoutManager. 每行的网格单元数是您的列数。标题行可以实现为不同的 ViewHolder,它使用不同的 ui,您对其余数据所做的就是将其放入一维数组中。
回答by Ingo Schwarz
If you want to have a completely dynamic table as you are used to by ListViewor RecyclerViewthere is the table view on GitHub.
如果你想拥有一个完全动态的表格,就像你习惯的那样,ListView或者RecyclerView在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)
由于该表提供了许多自定义和样式可能性,因此结果可能如下所示:(或完全不同)
Best regards :)
此致 :)
回答by cutiko
You can create any view programatically from Java, by example: TextView tv = new TextView(context);
您可以从 Java 以编程方式创建任何视图,例如: TextView tv = new TextView(context);
So you can do that for the the Tableand for their rows and columns.
因此,您可以Table为它们的行和列执行此操作。
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:
一旦你创建了它,你必须将它添加到你的布局中,你必须找到根 xml 元素,然后添加到它:
Viewgroup root = findeViewById(R.id.root);root.addViews(programaticView);
Viewgroup root = findeViewById(R.id.root);root.addViews(programaticView);


