【问题标题】:Populating a table with an array of data用一组数据填充表
【发布时间】:2015-05-23 11:31:59
【问题描述】:

我想建立一个表格,它有固定数量的列和 X 数量的行,第一行列出每列的内容。例如,一列是“姓名”,第二列是“年龄”,那么将有 X 行存储数据。有什么方法可以在其他地方为这些数据设置数组,并用这些数据自动创建/填充表的行。我之前使用自定义适配器通过更简单的示例完成了此操作,但我不太确定如何处理涉及的表。我很困惑,任何帮助将不胜感激。

【问题讨论】:

    标签: android listview android-tablelayout populate


    【解决方案1】:

    我认为在表格布局中创建如下表格行

    <TableLayout
    ---
    >
    <TableRow
    --
    >
    <Textview
    for name
    />
    <Textview
    ...for age
    />
    
    </TableRow>
    <TableRow
    --
    >
    <Listview
    for name
    />
    <Listview
    ...for age
    />
    
    </TableRow>
    
    </TableLayout>
    

    并使用您的固定数据使用简单的 arrayadapter 填充列表视图。

    如果您的列表视图中只有文本视图可以使用,这很简单

    ArrayAdapter ad=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,namearray); list1.setAdapter(ad);
    
    ArrayAdapter ad=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,agearray); list2.setAdapter(ad);
    

    【讨论】:

    • 感谢您的帮助,但我已经能够做到这一点。我想知道的是如何使用链接到对象数组的某种适配器自动填充每一行中的数据。非常感谢任何帮助!
    【解决方案2】:

    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 中的值设置列表视图中的字段。网上有很多关于这个的教程。

    【讨论】:

    猜你喜欢
    • 2011-02-02
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多