2012-03-13 3 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ビューで移入する必要がありながら、だから、私は唯一のショーアップしています。興味深いのは、ランナーがカウントアップするにつれて遅延が観測できることですが、もう一度質問ビューが1つしか表示されないことに注意してください。私が物事を変更すると、私はTableLayoutの周りに多くのヌルポインタを取得するので、私はその部分を削除しました。

最終的にEconFragmentのxmlにはScrollViewのテーブルレイアウトがあり、もう1つはTableLayout、questionContainerです。

元々、各質問画面をquestionContainerの行として追加したいと考えていましたが、それは私にあらゆる種類のトラブルを与えています。私はonCreateView()のreturn文の前にpopulateQuestions()と呼ぶことに幾分関係があると思いますか?

答えて

1

ここで私はどのように問題を解決したのですか。これは、私が望むSQL dBアクセスを実装していません。それは、フィラーwhileループとテーブル行を作るための文字列のダミー配列を持っています。私はほぼSQL dBを介してログイン/登録を実装しています。そして、すぐに、これらのtablerowを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"); 
    } 
関連する問題