0

我正在尝试在 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。我的计划是应用样式以使警报对话框中的列表片段和按钮显示为连续列表。提前致谢。

4

1 回答 1

-1

使用片段对话框显示列表项。 如何在 DialogFragment 中显示现有的 ListFragment。这个链接应该可以帮助你。

于 2012-11-05T04:01:24.273 回答