Building table layout dynamically at run time from XML

时间:2016-02-03 03:07:15

标签: android xml

New to android, I would like to have a view like below with actual values.

enter image description here

My code to dummy values is as below:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp">

    <TableRow
        android:background="#607D8B"
        android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Title" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Due On" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />
    </TableRow>

    <TableRow
        android:background="#ECEFF1"
        android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Access to Knowledge" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="12 Feb 2016" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Renew" />
    </TableRow>

    <TableRow
        android:background="#ECEFF1"
        android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Achivement" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="16 Feb 2016" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Renew" />
    </TableRow>
    <TableRow
        android:background="#ECEFF1"
        android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="God of small things" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="22 Feb 2016" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Renew" />
    </TableRow>

    <Button
        android:text="Renew all" />

</TableLayout>

I am getting the values from XML from webservices and parsing it to show in my table view.

I am using DocumentBuilderFactory to parse my XML. Here my XML file is in String str.

 builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                            InputSource src = new InputSource();
                            src.setCharacterStream(new StringReader(str));

                            Document doc = builder.parse(src);
                            String title = `doc.getElementsByTagName("title").item(0).getTextContent();`

Normally I use

TitleTxt.setText("Title: " + title);

PublisherTxt.setText("Publisher: " + publisher)

to show the results in my view.

I have multiple questions: Q1 what modifications are needed in layout file to show - actual values and not the dummy values? Q2 how to get the array values in XML parsing to show in my layout file? Q3 how to show the XML parsed results in my table view - dynamically at runtime?

These are related queries, so had to put in the same question. Some of it may sound simple, but being new to Android, I am not able to figure out. Also tried to find a sample code to do it but could not find. Not sure if I am using the right keywords to search for what I am looking for.

Any links to sample code or which exact keywords to look for to search my issue solution or suggestions will also be appreciated.

Thanks in advance.

1 个答案:

答案 0 :(得分:0)

为了在运行时将新视图添加到另一个,您可以使用 View.add 方法。因此,为了使上表“动态”,您可以将布局文件更改为仅包含TableLayout。稍后,在运行时,您以编程方式将TableRows添加到TableLayout。 基本上,有两个选项如何添加TableRow。 首先,您可以构建TableRow,例如:

TextView tvTitle = new TextView(...);
TextView tvDueOn = new TextView(...);
Button btnRenew = new TextView(...);
TableRow tr = new TableRow(...);
tr.add(tvTitle);
tr.add(tvDueOn);
tr.add(btnRenew);
TableLayout lytTable = (TableLayout)findViewById(...);
lytTable.add(tr);

这只是一个最小版本,但通过添加视图权重,layout_width和layout_height,您可以实现此目的。

第二,您可以在布局XML文件中设计TableRow,然后可以使用LayoutInflater在运行时加载并添加到TableLayout。

lytTableRow.xml

<TableRow
    android:id="+@id/lytTableRow"
    android:background="#607D8B"
    android:padding="5dp">
    <TextView
        android:id="+@id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <TextView
        android:id="+@id/tvDueOn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="+@id/btnRenew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="" />
</TableRow>

活动中的代码

TableLayout lytTable = (TableLayout)findViewById(R.id.lytTable);
View tableRow = getLayoutInflater().inflate(R.layout.lytTableRow, null);
TextView tvTitle = (TextView)tableRow.findViewById(R.id.tvTitle);
TextView tvDueOn = (TextView)tableRow.findViewById(R.id.tvDueOn);
Button btnRenew = (Button)tableRow.findViewById(R.id.btnRenew);
tvTitle.setText(...);

例如,两个代码部分都可以放入循环中,以便添加多行 - 但要注意使用view.setTag将标记 / ids设置为您的视图才能成为稍后可以在Button Click listener中区分它们。

关于数据,根据您使用的XML解析器类型,您可以遍历数据中的每个项目,并使用上面显示的选项之一将它们添加到TableLayout。根据您的问题,您使用DOM解析器。您可以使用从doc.getElementsByTagName("parentnodename")检索到的NodeList循环遍历项目,其中parentnodename是包含“title”,“dueon”等的元素名称。