2013-10-10 5 views
0

소개 탐색 창에서 사용자의 선택에 따라 다른 조각을 만들고 싶습니다. 탐색 서랍의 모든 항목에 대해 호출해야하는 다른 사이트가 있습니다. 나는 조각으로 이것을 달성하고 싶다. 주요 조각이 당신은 내가 완벽하게 작동 조각하는 방법에 데이터 (해시 맵)을 통과하고있어 볼 수 있듯이에서 onCreate 메소드조각에 레이아웃을 추가 프로그래밍 방식

if (savedInstanceState == null) { 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    Fragment fragment = MainFragment.passList(hashMap); 
    ft.replace(R.id.content_frame, new MainFragment()); 
    ft.commit(); 
} 

에서 다음을 수행하여 먼저 표시 얻을 것이다. 데이터는 문자열 배열에 저장됩니다. 이 작동하고 데이터를 조각의 onCreate 메서드에서 사용할 수도 있습니다.

는 주요 활동

<android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

<FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/darker_gray" 
     android:dividerHeight="0.1dp" /> 

</android.support.v4.widget.DrawerLayout> 

의 XML입니다 그리고 이것은

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainRelativeLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?android:attr/activatedBackgroundIndicator" 
    android:gravity="center_vertical" 
    android:paddingRight="10dp" 
    > 
<ScrollView 
     android:id="@+id/mainScrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1.03" > 

     <LinearLayout 
      android:id="@+id/mainListView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </ScrollView> 
    </RelativeLayout> 

은 내가 지금 달성하고자하는 스크롤 뷰에 데이터를 표시하는 것입니다 주요 조각의 XML입니다. 하지만 내가 가지고있는 질문은 어떻게 그것을 채울 수 있으며 어떻게 초기화해야합니까?

일반적으로 나는 갈 것이다 및 단지를 만드는 것과 같은 방법으로 생성 - findViewById를이 조각에서 작동하지 않기 때문에

ScrollView sv = new ScrollView(getActivity()); 
LinearLayout ll = new LinearLayout (getActivity()); 

sv.addView(ll); 
ll.addView(SAMPLETEXTVIEW); 

View mainView = inflater 
      .inflate(R.layout.main_fragment, container, false); 
    return mainView; 

(내 자신에 의해 방법을 쓸 필요)하지만 그냥 빈 화면이 나 잎 실제 텍스트가 추가되지 않았습니다. 이것을 어떻게 할 수 있습니까? 데이터는 거기에 있습니다. System.out.println을 사용하여 인쇄되기 때문에 그것을 표시 할 수있는 방법이 필요합니다. 미리 Thx!

편집

내가있는 ScrollView는 하나의 직접 아이를 호스팅 할 수 있습니다 단지 (자), IllegalStateException를 얻을

View rootView = inflater.inflate(R.layout.main_fragment, container, false); 

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView); 
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView); 

    sv.addView(ll); 

처럼 할 ...하지만 내가 그것을 얻을하지 않는 경우. 선형 레이아웃은 직접적인 자식입니다. 왜 문제가 있습니까? 여기

는 이미 이미 하나 직접 아이 (선언하였습니다있는 ScrollView를 포함하는 당신의 R.layout.main_fragment을 팽창하기 때문에 IllegalStateException가 얻을 전체 onCreateView -method

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.main_fragment, container, false); 

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView); 
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView); 

    sv.addView(ll); 
    source  = new TextView[hashMapSize]; 


    for (int i = 0; i < hashMapSize; i++) { 

     source[i]  = new TextView(getActivity()); 
source  [i].setText("Source: "  + sourceArr  [i]); 
ll.addView(source[i] 

    } 
    return rootView; 
} 

답변

1

이유입니다 레이아웃 xml).

sv.addView(ll) 코드를 추가해서는 안됩니다. 이미 있습니다. findViewById으로 이미 수행 한 것처럼 참조를 얻으십시오.

sv.addView(ll);을 제거하면 문제가 없습니다.

+0

오 세상에, 너무 단순 .... 감사합니다! – Musterknabe

+1

때로는 신선한 쌍안경이 필요합니다. :) – linakis