2014-09-26 3 views
0

이 질문은 이전에 게시 한 질문과 관련되어 있습니다. Views removed using removeView() are there when I next open the Activity (Android)getChildCount()는 TableLayout에서 호출 될 때 0을 반환합니다.

배경 : 사용자가 내 앱에 로그인하면 로그인 활동에서 기본 페이지 활동으로 이동합니다. 메인 페이지에는 동적으로 생성 된 버튼이 포함 된 TableLayout이 있습니다. 그러나 사용자가 로그 아웃했다가 다시 들어 오면 모든 단추가 반복되므로 생성 된 단추를 제거한 후 가장 좋은 방법을 찾으려고합니다. 이전 게시물에서는 새 페이지가 그려지기 전에 주 페이지 활동의 맨 처음에 단추를 제거 할 것을 제안 했으므로 이것이 구현하려고하는 것입니다.

그러나이 레이아웃에서 getChildCount()를 호출하면 항상 올바른 대답을 반환하지는 않습니다.

지금까지, 여기에 메인 페이지 활동의 시작시에 실행되는 코드입니다 :

 TableLayout tableLayout = (TableLayout)findViewById(R.id.MainPageTableTitle); 
     //removeSectionButtons(tableLayout); this is where i am trying to remove the buttons 
     System.out.println("there are oncreate " + tableLayout.getChildCount()); 
     drawButtons(tableLayout); 
     System.out.println("there are ondraw " + tableLayout.getChildCount()); 

첫 번째 인쇄 라인은 0 반환하고 두 번째 인쇄 라인은 항상 (버튼의 수를 정답을 반환 반복 된 모든 것을 포함하여 그려진다. 하지만 getChildCount()가 처음으로 잘못된 대답을 반환하는 이유를 모르겠습니다.

 public void drawButtons(TableLayout tableLayout){ 
    //get the number of buttons 
    int noOfButtons = mySectionTableHandler.getSectionDetails().size(); 
    //calculate the number of rows needed (there are 2 columns) 
    //set flag to say if buttons are odd as it affects how many are drawn 
    int noOfRows; 
    boolean evenNoOfButtons; 
    if(noOfButtons % 2 == 0){ 
     //even no of buttons 
     noOfRows = noOfButtons/2; 
     evenNoOfButtons = true; 
    } else { 
     //odd no of buttons 
     noOfRows = (noOfButtons+1)/2; 
     evenNoOfButtons = false; 
    } 

    //counter to give each button a unique id 
    int counter = 1; 

    for(int i = 0; i<noOfRows;i++){ 
     TableRow newRow = new TableRow(this); 

     Button a = new Button(MainPageActivity.this); 
     a.setId(counter); 
     sectionButtons.put(counter, a); 
     counter++; 
     newRow.addView(a); 

     //if there are even buttons OR if there are an odd no 
     //of buttons but this isn't the last row then add 
     //second button to row 
     if(evenNoOfButtons || (!evenNoOfButtons && (noOfRows-1!=i))){ 
      Button b = new Button(MainPageActivity.this); 
      b.setId(counter); 
      sectionButtons.put(counter, b); 
      counter++; 
      newRow.addView(b); 
     }  
     tableLayout.addView(newRow); 
    } 

} 

답변

0

이 내 나쁜, 내가 데이터를 재 추가되었다 밝혀 : 사람이 내가 다음과 같이

내 drawButtons() 메소드는 믿을 수 없을만큼 감사 할 것입니다 설명 할 수있는 경우 (이 행 당 두 개의 버튼을 그립니다) 사용자가 이전 세부 사항을 지우지 않고 로그인 할 때마다 sqlite 데이터베이스에 저장됩니다.

그래서 내 코드는 데이터베이스의 모든 필드에 대해 버튼을 생성해야합니다.

관련 문제