2
我試圖向我的動態創建的TableLayout添加一些隨機數據,以查看它是否正常工作。將TableRows動態添加到片段
下面是代碼
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] = "[email protected]";
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>
這種方法卻行不通(顯示除了照片是Nothing) 我一個相對的初學者在使用片段,我也困惑的是其中函數(onCreate(),onCreateActivity()等)我應該把上面的代碼填充我的表。
任何幫助表示讚賞。
如果我的問題看起來過於愚蠢,我很抱歉。
你的代碼對我幫助很大。當我整天用填充動態表的方式浪費我的時間。我的應用程序崩潰,只是因爲我沒有注意到我正在使用片段。 非常感謝你:) – MajorGeek 2013-12-29 16:33:50