I'm trying to show a ListFragment in an AlertDialog. The ListFragment appears as expected when called from an activity, but it does not appear in the dialog.
Hers is the Alert:
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;
}
}
Here is the AlertDialog layout:
<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>
And here is the 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;
}
...
}
Not sure if I need to call the ListFragment in a different way since it is an AlertDialog as opposed to an Activity. My plan is to apply styles to make the list fragment and buttons in the alert dialog appear as a continuous list. Thanks in advance.
Use a fragment dialog to display list items. How to display an existing ListFragment in a DialogFragment. This link should help you.