我想建立一个表,该表具有固定的列数和X的行数,其中第一行列出了每一列的内容。例如,一列将是“名称”,第二列将是“年龄”,然后将有X的行存储数据。有什么办法可以在其他地方为此数据设置数组,并自动使用该数据创建/填充表的行。之前,我已经使用自定义适配器通过一个更简单的示例完成了此操作,但是我不确定如何在涉及的表中进行此操作。我很困惑,任何帮助将不胜感激。
最佳答案
ListView
基本上充当任何数据表中的一行。您应该使用要连续显示的所有属性创建一个POJO。您可以为数据创建一个自定义xml布局,该布局可能会通过与列对应的水平LinearLayout
来实现。
POJO
public class MyData {
public String Name;
public int Age;
// More data here
}
ListView项目布局(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中的值设置列表视图中的字段。互联网上有很多有关此的教程。
关于android - 用数据数组填充表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29160061/