public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// fragment에서 findViewById는 그냥 쓰면 안 먹힌다. 아래 과정을 해줘야 사용 가능하다.
View v = inflater.inflate(R.layout.fragment_food_calorie, container, false);
TableLayout table = (TableLayout)v.findViewById(R.id.TableLayout);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1,1,1,1);
tableRowParams.weight = 1;
// 데이터베이스 등록된 레코드 개수를 가져온다. 나중에 결과값을 받도록 수정할 것
int count = 10;
// 테이블의 라인을 동적으로 생성하는 for문
for(int i=0; i<count; ++i){
TableRow row = new TableRow(getActivity()); // getActivity()는 현재 실행중인 액티비티를 가져오는거다. fragment에서 this로 하면 안 된다.
// 테이블 행 설정
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// 음식 이미지나오는 이미지뷰
// 음식 칼로리 나타는 텍스트뷰
TextView tv = new TextView(getActivity());
tv.setText(i);
row.addView(tv, tableRowParams);
table.addView(row, tableLayoutParams);
}
return v;
//return inflater.inflate(R.layout.fragment_food_calorie, container, false);
}
fragment에서 동적으로 테이블을 생성하려고하는데 row.addView(tv, tableRowParams); 이 부분에서 디버깅이 멈추는데 어떤 이유인지 잘 모르겠네요...
다른 소스코드를 참고하는데 구버전이랑은 호환이 잘 안되는거같아요..