2012-11-05 29 views
0

我試圖在AlertDialog中顯示一個ListFragment。當從活動調用ListFragment時,按預期顯示,但它不出現在對話框中。在AlertDialog中顯示一個ListFragment

她的字提醒:

public class ProfileSelectFragment extends DialogFragment { 
    private static final String DEBUG_TAG = "ProfileSelectFragment"; 
    protected int layout = R.layout.profileselect_dialog; 
    final Fragment fragment0 = new LoadedProfilesFragment(); 
    private Button loginButton; 
    private Button createProfileButton; 
    private Button anonymousButton; 

    static ProfileSelectFragment newInstance() { 
     ProfileSelectFragment f = new ProfileSelectFragment(); 
     return f; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     LayoutInflater inflater = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
     View customView = inflater.inflate(layout, null, false); 

     ... 

     Dialog myDialog = new AlertDialog.Builder(getActivity()).setView(customView) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d(DEBUG_TAG, "cancel"); 
        } 
       }).create(); 
     final android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager(); 
     final android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction(); 
     transaction.replace(R.id.fragment, fragment0); 
     transaction.commit(); 
     return myDialog; 
    } 
} 

這裏是AlertDialog佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <FrameLayout 
     android:id="@+id/fragment" 
     android:layout_width="fill_parent" 
     android:layout_height="80dip" 
     android:background="#33CC33"> 
    </FrameLayout> 
    <Button 
     android:id="@+id/button1" 
     style="@android:style/Holo.ButtonBar.AlertDialog" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/showlogin" /> 
    <Button 
     android:id="@+id/button2" 
     style="@android:style/Holo.ButtonBar.AlertDialog" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/createprofile" /> 
    <Button 
     android:id="@+id/button3" 
     style="@android:style/Holo.ButtonBar.AlertDialog" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/anonymous" /> 
</LinearLayout> 

這裏是ListFragment:

public class LoadedProfilesFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { 
    private static final String DEBUG_TAG = "LoadedProfilesFragment"; 
    private static final int LIST_LOADER = R.loader.loadedprofilesfragloader; 
    protected int layout = R.layout.log_fragment; 
    protected int entryLayout = R.layout.list_item; 
    protected final Uri table = ProfileProvider.URI_LOADEDPROFILEVIEW; 
    protected SimpleCursorAdapter listAdapter; 
    protected String[] uiBindFrom = { ProfilesColumns.USERNAME, ProfilesColumns.CREATIONDATE }; 
    protected int[] uiBindTo = { R.id.title, R.id.creationdate }; 
    protected String[] createProjection = { CommonDatabaseHelper._ID, ProfilesColumns.USERNAME, 
      ProfilesColumns.CREATIONDATE }; 

    public LoadedProfilesFragment() { 
     super(); 
    } 

    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getLoaderManager().initLoader(LIST_LOADER, null, this); 
     listAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), entryLayout, null, uiBindFrom, 
       uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 
     setListAdapter(listAdapter); 
    } 

    @Override 
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { 
     final View view = inflater.inflate(layout, container, false); 
     return view; 
    } 

    ... 

} 

不知道如果我需要調用因爲它是AlertDialog而不是Activity,所以ListFragment的方式不同。我的計劃是應用樣式以使警報對話框中的列表片段和按鈕顯示爲連續列表。提前致謝。

回答