2012-03-13 4 views
1

비슷한 질문을 살펴본 결과 아직 답을 찾지 못했습니다. 나는 결국 각각의 View을 SQL DB로부터 데이터를 획득 한 후에 추가 할 것이지만, 지금은 레이아웃을 추가 할 수 있도록 뷰 세트를 추가하려고합니다. 필자의 탭을 흘려 보내고로드하는 조각 관리자가있는 주요 활동이 있으므로 여기서 코드를 제공해야한다고 생각하지 않습니다. 이 컨테이너에 두 개 이상의 Question.xml보기에 채워야 동안 런타임 중에 조각 내에서 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++; 
    } 
} 
} 

그래서, 나는 단 하나 개의 쇼 최대가 : 여기

은 조각에 대한 코드입니다. 주자가 증가 할 때까지 지연을 관찰 할 수 있지만 재미있는 점은 하나의 질문보기 만 나타납니다. 나는 물건을 바꾸면, 내 TableLayout 주위에 널 포인터가 많이 생겨서 그 부분을 삭제했다.

궁극적으로 EconFragment의 xml에는 ScrollView 인 테이블 레이아웃이 있으며 TableLayout, questionContainer가 있습니다.

원래 각 질문보기를 questionContainer의 행으로 추가하기를 원했지만 모든 문제가 발생했습니다. 나는 onCreateView()에 return 문 앞에 populateQuestions()을 호출하는 것과 다소 관련이 있다고 가정합니다.

답변

1

다음은 문제를 해결 한 방법입니다. 필자는 원하는 필자의 SQL dB 액세스를 구현하지 않습니다. 필자는 while 루프와 더미 행 배열을 사용하여 테이블 행을 만듭니다. SQL/DB를 통해 로그인/등록을 구현하는 일이 거의 끝났습니다. 끝내 자마자 SQL을 통해 이러한 tablerow를 채우는 것이 큰 문제가되어서는 안됩니다.

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"); 
    } 
관련 문제