以编程方式创建多个LIst视图或表格布局

时间:2014-11-15 03:39:37

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

我已阅读有关以编程方式创建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>

1 个答案:

答案 0 :(得分:0)

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

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

使用ListView方法,听起来应该使用SimpleAdapter。不是非常普遍,因为它们仅对某些情况有用。老实说,名称应该是ListMapAdapter,因为它存储和期望数据传递给它......通过List的{​​{1}},然后使用Maps更加困难因为它有一个最令人困惑的构造函数。基本上ArrayAdapter非常适合渲染类似于布局的表格,其中包含数据列。

这是一个基本的例子,让您了解如何使用。这适用于直接向TextView显示SimpleAdapter数据。

Strings

更高概述//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> 是否适用于您的数据更依赖于细节。使用SimpleAdapterArrayAdapter等同样可行。这一切都归结为您的数据组织方式。此外,如果您需要以更复杂的方式显示数据,那么只需要字符串...您可能需要覆盖适配器“CursorAdapter ...”如果是这样的话,一定要确保使用适配器,数据组织最符合您的要求。

相关问题