我看过了类似的问题,但还没有找到答案。在从SQL中获取数据之后,我最终将添加我的每个View,但现在,我只是尝试添加一组视图,以便我的布局能够正常工作。我有一个片段管理器的主要活动,它使我的选项卡流动并加载得很好,所以我不需要在这里提供这些代码。
下面是片段的代码:
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视图中填充到容器中,但我只显示了一个。有趣的是,我可以观察到当跑步者增加到计数时的延迟,但同样,只有一个问题视图出现。当我更改内容时,我在我的TableLayout中得到了很多空指针,所以我删除了这个部分。
最终,EconFragment的xml有一个带有ScrollView的表布局,其中有另一个TableLayout,questionContainer。
我原本希望在questionContainer**,中将每个问题视图作为一行添加到中,但这给我带来了各种麻烦。我假定这与在populateQuestions() onCreateView()**?**中返回语句之前调用**有一定关系
发布于 2012-03-30 06:50:35
我就是这样解决这个问题的。它没有实现我想要的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");
    }https://stackoverflow.com/questions/9693543
复制相似问题