마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

MainView의 child를 Remove시키려고 하는데 반복문을 돌리면 이상하게 삭제가되네요.

0 추천

MainView에 7개의 자식뷰가 있습니다. 그중 6개는 동적으로 생성한뒤 Tag값으로 제어를 하고 있고요 

나머지 하나는 xml에 선언한뒤에 R.id를 받아서 Tag값을 선언한뒤 제어하고 있습니다.

처음에는 화면이 갱신될떄 6개의 자식뷰는 사라져야하지만 마지막 7번째 뷰는 삭제되지 않아야해서 

루프로 돌려서 7번째 태그 가 아닌것들만 제거를 하려고 했더니 Main_View.getChildCount() 를 돌려서 태그 값을 출력해본결과 이상하게 7번째 태그값이 없더라고요 (All Remove 하게되면 7개의 뷰가 다사라짐) 

그래서 FOR루프로 태그값 6개를 전부 제거시키려고 하는데 루프를 돌릴때 remove함수를 쓰지 않으면 6개 정상 출력되지만 

remove 함수를 쓴뒤에 돌리면 6개중에 3개밖에 지워지지 않아요 그이유를 모르겠습니다.

 

remove 함수를 쓰지 않은 루프

    int Child = Main_View.getChildCount();
               for(int cnt=0; cnt<Child; cnt++)
               {
 
                           Log.e("TAG", "제거된 TAG :" + Main_View.getChildAt(cnt).getTag() + "CNT:" + cnt +                               "Main_View.getChildCount():" + Main_View.getChildCount());
               }
 
logcat
04-07 17:32:08.850  16058-16058/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile:0CNT:0Main_View.getChildCount():6
04-07 17:32:08.850  16058-16058/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:1CNT:1Main_View.getChildCount():6
04-07 17:32:08.850  16058-16058/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:2CNT:2Main_View.getChildCount():6
04-07 17:32:08.850  16058-16058/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:3CNT:3Main_View.getChildCount():6
04-07 17:32:08.850  16058-16058/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:4CNT:4Main_View.getChildCount():6
04-07 17:32:08.850  16058-16058/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:5CNT:5Main_View.getChildCount():6

 

remove를 쓴 루프 

               int Child = Main_View.getChildCount();
               for(int cnt=0; cnt<Child; cnt++)
               {
                          Main_View.removeView(Main_View.getChildAt(cnt));
                           Log.e("TAG", "제거된 TAG :" + Main_View.getChildAt(cnt).getTag() + "CNT:" + cnt + "Main_View.getChildCount():" + Main_View.getChildCount());
               }
 
logcat
 
04-07 17:31:29.235  15894-15894/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:1CNT:0Main_View.getChildCount():5
04-07 17:31:29.235  15894-15894/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:3CNT:1Main_View.getChildCount():4
04-07 17:31:29.235  15894-15894/kr.qoop.contacts E/TAG﹕ 제거된 TAG 있나:Profile_Add:5CNT:2Main_View.getChildCount():3
 
왜 이런건지 모르겠습니다 ㅠ

 

 

// Child 지운 부분 code

    public void mOnClick(View v)
    {
       switch (v.getId())
       {
           case R.id.Profile_Box_Okay:
                Profile_Box_Flag=false;
                Profile_Info_Layout.setVisibility(View.GONE);
               BitmapDrawable Poto = (BitmapDrawable)((ImageView)findViewById(R.id.Profile_Poto)).getDrawable();
               Bitmap poto= Poto.getBitmap();
               Profile_Change_Set(RequestCode_code,poto,Profile_Name.getText().toString(),Profile_Phone.getText().toString());
               Toast.makeText(Main.this,"Okay",Toast.LENGTH_SHORT).show();
 
               int Child = Main_View.getChildCount();
               for(int cnt=0; cnt<Child; cnt++)
               {
                        Main_View.removeView(Main_View.getChildAt(cnt));
                           Log.e("TAG", "제거된 TAG :" + Main_View.getChildAt(cnt).getTag() + "CNT:" + cnt + "Main_View.getChildCount():" + Main_View.getChildCount());
               }
               break;
           case R.id.Profile_Box_Cancel:
               Profile_Box_Flag=false;
               Profile_Info_Layout.setVisibility(View.GONE);
               Toast.makeText(Main.this,"Cancel",Toast.LENGTH_SHORT).show();
               break;
           case R.id.Profile_Poto:
               Toast.makeText(Main.this,"Poto",Toast.LENGTH_SHORT).show();
               break;
       }
    }
 
익명사용자 님이 2015년 4월 7일 질문
2015년 4월 7일 수정

2개의 답변

+1 추천
 
채택된 답변
child 를 어디서 어떻게 지웠는지가 코드에 안나와있네용

그부분이 나와야 말씀을 드릴 수 있을 것 같네요
mamondebaltob (32,750 포인트) 님이 2015년 4월 7일 답변
답변감사드립니다. 질문 수정했습니다. 각각의 child는 RelativeLayout으로 이루워져 있구요. 7번째 뷰에 Profile_Box_Okay,Profile_Box_Cancel,R.id.Profile_Poto 버튼이 정의되어 있습니다.Profile_Box_Okay를 누를경우 MainView.getChildCount()를 이용하여 for루프를 돌리면서 MainView에 있는 View를 제거 하고 있습니다. ㅠ
for loop 에서 remove 를 해서 그러는데요
child index 가 0 1 2 3 4 5 6 이리 있다면
1 을 지우고 다음 룹으로 갔을때 2 의 child 뷰가 그대로 있는게 아니라
0 1 2 3 4 5 이리 되겠죠
다시 말해서 index 1을 지웠을 때 다음 index 2 는 과거의 index 3 을 참조하는거죠
ViewGroup.removeViewsInLayout(start, count);
를 사용하시든지 지우고 싶으신 child 에 공통된 태그를 적용해
불러서 지우든지 하세용
removeViews 가 더 낫겠네요
답변감사드립니다 덕분에 해결했습니다 ㅎ
해결 방법
  int Child = Main_View.getChildCount();
               int cnt=0;
               boolean Flag=false;
               while(cnt<Child)
               {
                   Main_View.removeView(Main_View.getChildAt(0));
                    cnt++;
               }
0 추천
for문을 3번 돌고 3개를 지워서 cnt가 3이 되면

자식뷰는 3개만 남겠죠

그러면

Main_View.removeView(Main_View.getChildAt(cnt));

여기에서 자식뷰는 0, 1, 2까지 있는데 3번자리에는 아무것도 없죠

그러니 안지워지는겁니다
alkyne (22,960 포인트) 님이 2015년 4월 7일 답변
답변감사드립니다. 덕분에 해결했어요 ㅎㅎ
...