gpt4 book ai didi

android - 将 TableRows 动态添加到 fragment

转载 作者:行者123 更新时间:2023-11-29 00:30:45 25 4
gpt4 key购买 nike

我正在尝试向我动态创建的 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() 等)中,以便它填充我的表。

感谢任何帮助。

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

最佳答案

不是再次膨胀 basic_details_layout,而是返回您已经膨胀的 View 。

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

应该是

return view;

关于android - 将 TableRows 动态添加到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15910457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com