android - 在运行时将 View 添加到 fragment 内的 TableLayout

标签 android user-interface android-fragments

我已经浏览过类似的问题,但尚未找到答案。从 SQL 数据库获取数据后,我最终会添加我的每个 View,但现在,我只是尝试添加一组 View ,以便让我的布局正常工作。我有一个 fragment 管理器的主要 Activity ,它让我的选项卡流动和加载得很好,所以我认为我不需要在这里提供该代码。

这是 fragment 的代码:

public class EconFragment extends Fragment {

private ViewGroup container;


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    populateQuestions(container, inflater);
    return inflater.inflate(R.layout.econfragment, container, false);

}


public void onPause() {
    super.onPause();
    container.removeAllViews();
}

private void populateQuestions(ViewGroup v, LayoutInflater inflater) {
    container = v;
    int count = 10;
    int runner = 0;


    while (runner < count) {
    View question = inflater.inflate(R.layout.question, null);
    question.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    container.addView(question, runner);
    runner++;
    }
}
}

因此,虽然这应该填充到容器中的多个 Question.xml View 中,但我只显示了一个。有趣的是,我可以观察到延迟,因为运行者增加到计数,但同样,只显示一个问题 View 。当我进行更改时,我的 TableLayout 周围有很多空指针,所以我删除了那部分。

最终,EconFragment 的 xml 有一个带有 ScrollView 的表格布局,后者有另一个 TableLayout,questionContainer。

我最初希望将每个问题 View 作为一行添加到 questionContainer 中,但这给我带来了各种麻烦。我认为这与在 onCreateView()?

中返回语句之前调用 populateQuestions() 有点相关

最佳答案

这是我解决问题的方法。它没有实现我想要的 SQL dB 访问,它只有一个填充 while 循环和一个虚拟字符串数组来制作表行。我几乎完成了通过 SQL DB 实现登录/注册,一旦完成,通过 SQL 填充这些表行应该不是什么大问题。

public class EconFragment extends Fragment {


    private TableLayout questionContainer;
    static int pos = 0;
    private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
            "hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("Econ", "onCreateView");

        return inflater.inflate(R.layout.econfragment, container, false);
    }

    public void onResume() {
        super.onResume();


        LayoutInflater inflater = (LayoutInflater) getActivity().
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        questionContainer = (TableLayout) getActivity().findViewById(R.id.questionContainer);



        //these lines are my first attempt to try and get the questions to repopulate after returning
        //from pressing back - as of yet, they don't repopulate the screen:
        //View econFragment = inflater.inflate(R.layout.econfragment, null);
        //container.addView(econFragment);

        int leftMargin=5;
        int topMargin=5;
        int rightMargin=5;
        int bottomMargin=5;
        while (pos < 10) {
        View question = inflater.inflate(R.layout.question, null);
        question.setId(pos);
        TextView title = (TextView) question.findViewById(R.id.questionTextView);
        title.setText(titles[pos]);
        Button charts = (Button) question.findViewById(R.id.chartsButton);
        charts.setId(pos);
        charts.setOnClickListener(chartsListener);
        TableRow tr = (TableRow) question;
        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
        trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trParams);

        questionContainer.addView(tr);
        pos++;
        }
        Log.d("Econ", "onResume");
    }

关于android - 在运行时将 View 添加到 fragment 内的 TableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9693543/

相关文章:

python - 如何使用 tkinter 显示函数调用的输出?

android - 使用 DialogFragment 创建自定义对话框

android - 自动对焦到下一个编辑文本字段

ios - UI 模板 View - iOs 应用程序所有场景中的通用 UI 元素?

java - 当我尝试设置 onClickListener 时应用程序崩溃

android - 如何在使用 Dagger 2 时将数据传递给子 fragment

android - 如何在一个在Android中也有侧面抽屉导航的 Activity 中制作标签?

android - 我想在android中创建垂直虚线

android - Azure DevOps 管道 android build设置 sdk 位置

android - Action Bar 是不可或缺的吗?