我正在尝试将一些随机数据添加到我动态创建的 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() 等)我应该把上面的代码,所以它填充我的表。
任何帮助表示赞赏。
如果我的问题看起来过于愚蠢,我提前道歉。