Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

android - DialogFragment : Using AlertDialog with custom layout

I'm rewriting my application with Fragments API support. In the original application I have an AlertDialog that is created like this :

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.button_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(view);
    ListView mListView = (ListView) view.findViewById(R.id.mblist);
    builder.setTitle(getString(R.string.sysinfo)).setNeutralButton(
            getString(R.string.okay), null);
    Cursor dcursor = propManager.propCursor();
    if (dcursor.moveToFirst()) {
        startManagingCursor(dcursor);
        String[] from = { Constants.NAME_DET,
                Constants.VALUE_DET };
        int[] to = { R.id.mbname, R.id.mbvalue };
        SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this,
                R.layout.info_list_row, dcursor, from, to);
        mListView.setAdapter(dadapter);
    }
    final Dialog dialog = builder.create();
    dialog.show();

How can I display the same dialog via a DialogFragment? I have read the documentation but have trouble figuring out how to proceed with this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm surprised that nobody answered. This is the solution:

public class PropDialogFragment extends DialogFragment {

    private PropDialogFragment() { /*empty*/ } 

    /** creates a new instance of PropDialogFragment */
    public static PropDialogFragment newInstance() {
        return new PropDialogFragment();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //getting proper access to LayoutInflater is the trick. getLayoutInflater is a                   //Function
        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.my_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(view);
        builder.setTitle(getActivity().getString(R.string.sysinfo)).setNeutralButton(
                getActivity().getString(R.string.okay), null);
        return builder.create();
    }
}

You can show the dialog using :

private void showDialog() {
    // DialogFragment.show() will take care of adding the fragment
    // in a transaction.  We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = PropDialogFragment.newInstance();
    newFragment.show(ft, "dialog");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...