【问题标题】:Creating numerous LIstViews or TableLayouts programatically以编程方式创建大量列表视图或表格布局
【发布时间】:2014-11-15 03:39:37
【问题描述】:

我已阅读有关以编程方式创建 TableLayout/ListView 的各种帖子,但我仍然不确定最佳途径。

在我看来,我有一门课程叫RunTests.java。一个测试计划可以包含多种测试类型,并且对于每种测试类型都需要一个结果表。所以假设有四个测试,分别命名为 A、B、C 和 D。

测试 A 完成后,理想情况下,我希望调用另一个类 (ProcessTable.java) 并将测试结果数组、测试列标题数组和测试行标题数组传递给它。然后该类将返回一个包含结果和标题的表格,并对其进行膨胀,使其在 XML LinearLayout 中可见,然后继续测试 B。

然后该过程将重复,下一个表格将出现在第一个表格下方,依此类推。

任何建议或示例都会很棒。

谢谢

尝试建议的答案:

我在 listView 上遇到错误,我不确定我在哪里定义它。我添加为现有布局的 XML 如下:

private void createTable() {

        String[] columns = {"row_header", "column_name1", "column_name2"};
        String[] results = {"","2000","98"};
        int[] to = { R.id.row_header, R.id.txt1 ,R.id.txt2};
        String[] rowHeadings = {"","Bandwidth","QoS"};

        List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        for(int i = 0; i < rowHeadings.length; i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(columns[0], rowHeadings[i].toString());
            map.put(columns[1], results[1]);
            map.put(columns[2], results[2]);
            data .add(map);
        }

         SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), data, R.layout.runtests, columns, to);         
         listView.setAdapter(adapter);
    }

XML:

 <LinearLayout android:id="@+id/resultsLayout"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center|top"
         android:layout_marginEnd="8dp"
         android:layout_marginLeft="8dp"
         android:layout_marginRight="8dp"
         android:layout_marginStart="8dp"
         android:background="@drawable/curvedbg"
         android:layout_marginTop="10dp"
         android:padding="5dp"
         android:weightSum="99">

    <TextView android:id="@+id/row_header" android:layout_weight="33" />
    <TextView android:id="@+id/txt1" android:layout_weight="33"/>
    <TextView android:id="@+id/txt2" android:layout_weight="33"/>

</LinearLayout>

【问题讨论】:

    标签: java android android-listview android-tablelayout android-inflate


    【解决方案1】:

    我的直觉告诉我你应该使用ListView。这将很好地适应为不同的数据类型动态生成Views。此外,如果您有相当大的结果列表无法显示在屏幕上,ListView 将自动处理滚动,并且只会将显示在屏幕上的数据保存在内存中。

    另一方面,您需要用ScrollView 包装TableLayout,它将保留内存中生成的所有Views...无论它们是否显示在屏幕上。您还需要设置大量样板代码以生成 Views 并将其附加到主 XML。简而言之,TableLayout 非常适合静态数据。

    使用ListView 方法,听起来您应该使用SimpleAdapter。不是很常见,因为它们仅在某些情况下有用。老实说,名称应该是 ListMapAdapter,因为这就是它存储和期望数据传递给它的方式......通过 ListMaps 并且它可能比 ArrayAdapter 更难使用,因为它有一个有史以来最令人困惑的构造函数。基本上,SimpleAdapter 非常适合呈现类似布局的表格,其中您有数据列。

    这是一个基本示例,可让您了解如何使用。这适用于将Strings 的数据直接显示到 TextView。

    //Columns is used to assign a column to a TextView.  So columns[1] will place all data 
    //found in column 1 into to[1]...which is the TextView R.id.txt1. Columns is also used
    //to map which data item belongs to a particular column
    String[] columns = {"row_header", "column_name1", "column_name2", "column_name3"}
    int[] to = { R.id.row_header, R.id.txt1 ,R.id.txt2, R.id.txt3};
    
    //This is where you will assemble the test data. Probably the most difficult part 
    //as you must convert your test results into a List of Maps.  Each index in the 
    //List represents a row of data. The Map represents all the columns of data for 
    //that row.
    List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    for(int i = 0; i < rowHeadings.size(); i++){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(columns[0], rowHeadings.get(i));
        map.put(columns[1], Your_data_for_column1);
        map.put(columns[2], Your_data_for_column2);
        map.put(columns[3], Your_data_for_column3);
        data .add(map);
    }
    
     SimpleAdapter adapter = new SimpleAdapter(getContext(), data, R.layout.list_layout, columns, to);         
     listView.setAdapter(adapter);
    

    list_layout.xml 的高级概览示例

    <LinearLayout>
        <TextView android:id="@+id/row_header"/>
        <TextView android:id="@+id/txt1"/>
        <TextView android:id="@+id/txt2"/>
        <TextView android:id="@+id/txt3"/>
    </LinearLayout>
    

    注意SimpleAdapter 是否适用于您的数据取决于更多细节。使用ArrayAdapterCursorAdapter 等同样可行。这一切都归结为您的数据是如何组织的。此外,如果您需要以更复杂的方式显示数据,那么只需使用字符串...您可能需要覆盖适配器的getView()...如果是这种情况,请务必使用其数据组织的适配器最适合你的。

    【讨论】:

    • 谢谢@Jay,我会在星期一回到这里,所以会试一试。看来你已经得到了我想要的。都将是字符串。理想情况下,我想添加背景和设置行颜色等,这可能在 ListView 中还是需要 TableLayout?
    • 在全球范围内,所有列或所有行都将获得一种颜色......是的。您只需在 list_layout.xml 中进行这些更改。但是,如果您希望不同的行具有不同的颜色,则需要覆盖适配器的 getView() 方法来进行调整。
    • 我已添加您建议的更改。开场帖已更新。我有一个现有的布局“runtests.xml”,我希望结果出现在其中。所以我已将 XML 添加到其中。我是否使用 listView 而不是线性布局?
    • 我也尝试过使用表格布局并在stackoverflow.com/questions/27029554/…添加了一个新的论坛问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多