Amix
Amix

Reputation: 17

how to use getLayoutInflater in fragment layout?

I need to use getLayoutInflater in fragment class but is has an error like below:

Error:(290, 17) error: method getLayoutInflater in class Fragment cannot be applied to given types;
required: Bundle
found: no arguments
reason: actual and formal argument lists differ in length

It is my code :

private void initDialog(){
    alertDialog = new AlertDialog.Builder(getContext());
    view = getLayoutInflater().inflate(R.layout.dialog_layout,null);
    alertDialog.setView(view);
    alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(add){
                add =false;
                mainListAdapter.addItem(et_country.getText().toString());
                dialog.dismiss();
            } else {
                mainListAdapter.mList.set(edit_position,et_country.getText().toString());
                mainListAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }

        }
    });
    et_country = (EditText)view.findViewById(R.id.et_country);
}

Upvotes: 0

Views: 1181

Answers (2)

Sohail Zahid
Sohail Zahid

Reputation: 8149

Use getActivity() when you are fragment as a reference also pass getActivity()to Builder(getActivity());

    alertDialog = new AlertDialog.Builder(getActivity());
    view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout,null);

Upvotes: 0

Piyush
Piyush

Reputation: 18923

Simply use:

LayoutInflater inflater = LayoutInflater.from(getActivity());

or

getActivity().getLayoutInflater().inflate(R.layout.dialog_la‌​yout,null);

Upvotes: 2

Related Questions