用数据数组填充表 [英] Populating a table with an array of data
问题描述
我想建立一个表,该表具有固定的列数和X的行数,其中第一行列出了每一列的内容.例如,一列将是名称",第二列将是年龄",然后将有X数量的行用于存储数据.有什么方法可以在其他地方为此数据设置数组,并自动使用该数据创建/填充表的行.之前,我已经使用自定义适配器通过一个更简单的示例完成了此操作,但是我不确定如何在涉及的表中进行此操作.我感到很困惑,任何帮助将不胜感激.
I want to set up a table which has a fixed amount of columns, and an X amount of rows with The first row listing the contents of each column. For example, one column will be 'Name' and a second column will be 'Age', then there will be an X amount of rows which store the data. Is there any way that I can set up arrays for this data elsewhere, and automatically create/fill the rows of the table with this data. I've done this previously with a more simple example using a Custom Adapter but I'm not quite sure how to go about this with a table involved. I'm quite stuck and any help would be greatly appreciated.
推荐答案
ListView
基本上充当任何数据表中的行.您应该使用要连续显示的所有属性创建一个POJO.您可以为数据创建一个自定义xml布局,该布局可能由与列对应的水平LinearLayout
来完成.
A ListView
basically acts as a row within any table of data. You should create a POJO with all the attributes you want to display in a row. You can create create a custom xml layout for the data, which would probably by a horizontal LinearLayout
that corresponds to your columns.
POJO
public class MyData {
public String Name;
public int Age;
// More data here
}
ListView项的布局(layout_list_item.xml)
ListView item layout (layout_list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Name"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.7" />
<TextView
android:id="@+id/Age"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.3" />
</LinearLayout>
主要布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.7"
android:text="Name" />
<TextView
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.3"
android:text="Age" />
</LinearLayout>
<ListView
android:id="+@id/MyDataListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
然后,您需要一个自定义适配器,该适配器使用POJO中的值设置列表视图中的字段.互联网上有很多关于此的教程.
You then need a custom adapter which sets the fields in the list view with the values from your POJO. There are plenty of tutorials on the internet for this.
这篇关于用数据数组填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!