将表行动态添加到片段

时间:2013-04-09 19:06:11

标签: android

我正在尝试将一些随机数据添加到我动态创建的TableLayout中,看看它是否正常工作。

这是代码

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class BasicDetailsTabFragment extends Fragment
{
    int numvals = 5;
    public static final int NAME = 0;
    public static final int BRANCH = 1;
    public static final int EMAIL = 2;
    public static final int REGISTRATION = 3;
    public static final int ADMISSION = 4;

    String[] details = new String[5];
    String[] detailVals = new String[5]; // Get the values from JSON here

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        if (container == null)
        {
            return null;
        }

        details[NAME] = "Name";
        details[BRANCH] = "Branch";
        details[EMAIL] = "Email";
        details[REGISTRATION] = "Registration No.";
        details[ADMISSION] = "Admission No. ";

        detailVals[NAME] = "Some Name";
        detailVals[BRANCH] = "Computer Science";
        detailVals[EMAIL] = "some_email@email.com";
        detailVals[REGISTRATION] = "11101212";
        detailVals[ADMISSION] = "1345645";

         View view = inflater.inflate(R.layout.basic_details_layout,
         container, false);

        TableLayout tl = (TableLayout)view.getView().findViewById(R.id.detailsTable);

        for (int i = 0; i < numvals; i++)
        {
            // Make TR
            TableRow tr = new TableRow(getActivity());
            tr.setId(100 + i);
            tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            // Make TV to hold the details
            TextView detailstv = new TextView(getActivity());
            detailstv.setId(200 + i);
            detailstv.setText(details[i]);
            tr.addView(detailstv);

            // Make TV to hold the detailvals

            TextView valstv = new TextView(getActivity());
            valstv.setId(300 + i);
            valstv.setText(detailVals[i]);
            tr.addView(valstv);

            tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }

        return (RelativeLayout) inflater.inflate(R.layout.basic_details_layout, container, false);

    }

这是这个类的布局的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/studentPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:src="@drawable/student_photo" />

    <TableLayout
        android:id="@+id/detailsTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/studentPhoto" >
    </TableLayout>

</RelativeLayout>

然而,此方法不起作用(除照片外不显示任何内容) 我是使用Fragments的相对初学者,我也很困惑的是其中的函数(onCreate(),onCreateActivity()等)我应该把上面的代码放在我的表中。

感谢任何帮助。

如果我的问题看起来过于愚蠢,我会提前道歉。

1 个答案:

答案 0 :(得分:2)

不要再次膨胀basic_details_layout,而是返回已经膨胀的视图。

return (RelativeLayout) inflater.inflate(R.layout.basic_details_layout, container, false);

应该是

return view;