2012-03-13 78 views
1

我已經查看了類似的問題,但尚未找到答案。我最終會在從SQL數據庫獲取數據後添加我的每個View,但現在,我只是試圖添加一組視圖,以便我的佈局能夠工作。我有一個使用片段管理器的主要活動,可以讓我的選項卡流動並加載得很好,所以我不認爲我需要在此處提供該代碼。在運行時在片段內添加視圖到TableLayout

下面是片段代碼:

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的行中添加每個問題視圖,但這給我帶來了各種麻煩。我認爲它在onCreateView()之前的返回聲明之前調用populateQuestions()有點相關?

回答

1

這是我如何解決問題。它並沒有實現我所希望的SQL dB訪問,它只是有一個填充while循環和一個虛擬字符串陣列來創建Table Rows。我幾乎完成了通過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"); 
    }