2013-06-02 1 views
2

다음 ListItem Listener가 있습니다. 목록 중 하나를 클릭하면 다른 옵션으로 다른 목록이 열리겠습니다.OnListItemClick 액션을 사용하여 새 ListFragment를 여는 방법?

public class MyList extends ListFragment { 

    ... 

    public void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     switch (position) { 
     case 0: 
      Intent newActivity = new Intent(v.getContext(), AnotherList.class); 
      startActivity(newActivity); 
     } 
    } 
} 

그리고 이것은 내가

public class AnotherList extends ListFragment { 

    ArrayList<String> storage = new ArrayList<String>(
      Arrays.asList("Test", "Test")); 
    ArrayAdapter<String> adapter; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     adapter = new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, storage); 

     setListAdapter(adapter); 

    } 
} 

나는 다음과 같은 오류 메시지가 첫 번째 옵션을 집어 때까지 열려는 다른 목록입니다

11월 6일부터 2일까지 생각 : 52 : 39.251 : E/AndroidRuntime (1231) : android.content.ActivityNotFoundException : 명시 적으로 찾을 수 없습니다 활동 클래스 {.....}; AndroidManifest.xml에서이 활동을 신고하셨습니까?

내 매니 페스트 파일에 AnotherList을 신고 했습니까? 이상한 나는 처음 ListFragment로 그렇게하지 않았기 때문이다.

UPDATE :

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.list_fragment, new AnotherList()); 
ft.commit(); 

OLD main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <fragment 
     android:id="@+id/list_fragment" 
     android:name="com.sanguosha.MyList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

새로운 작업 main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:id="@+id/list_fragment" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" ></LinearLayout> 

</LinearLayout> 

답변

2
  1. Intent newActivity = new Intent(v.getContext(), AnotherList.class); 
    startActivity(newActivity); 
    

이 잘못된 코드 조각을 조각되지 않은 다른 조각

  • 를 시작하기 위해 활동해야한다. 당신은 조각이 아닌 활동을 시작하려면 startActivity을 사용해야합니다. 조각을 사용하려면 FragmentTransaction

  • +0

    을 사용해야하지만 이전 목록을 대체하지는 않았지만 새 목록이 겹쳐졌습니다. 내 코드를 업데이트했습니다 – starcorn

    +0

    Activity가 관리하는 컨테이너가 있어야합니다. 액티비티는 컨테이너 내용을 대체해야합니다. – Blackbelt

    +0

    이제 작동합니다. 'main_activity.xml' 레이아웃을 조정해야했습니다. 왜 그것이 왜 필요한지 나는 잘 모르겠다. – starcorn