在AlertDialog中显示ListFragment

时间:2012-11-05 03:55:34

标签: android android-alertdialog android-listfragment

我正在尝试在AlertDialog中显示ListFragment。从活动调用时,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;
    }

    ...

}

不确定我是否需要以不同的方式调用ListFragment,因为它是AlertDialog而不是Activity。我的计划是应用样式使警报对话框中的列表片段和按钮显示为连续列表。提前谢谢。

1 个答案:

答案 0 :(得分:-1)

使用片段对话框显示列表项。 How to display an existing ListFragment in a DialogFragment。这个链接可以帮到你。