Reputation: 20646
I want to implement an Alert Dialog inside a Fragment.
I've created a new class called MyDialog
that contains :
public class MyDialog {
public static AlertDialog.Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
View view = layoutInflater.inflate(R.layout.userdialog, null);
((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
return new AlertDialog.Builder(context).setView(view);
}
}
Then at the time when I want to use it I call this class like :
AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), R.string.cambioemail, R.string.nuevoemail);
myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancelaruserdialog, null)
.show();
Full code of the Fragment :
public class ConfiguracionFragment extends Fragment implements View.OnClickListener {
public ConfiguracionFragment() {
}
TextView tvEmail,tvPswd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
tvEmail = (TextView) rootView.findViewById(R.id.tvEmailChange);
tvPswd = (TextView) rootView.findViewById(R.id.tvPswdChange);
tvEmail.setOnClickListener(this);
tvPswd.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvEmailChange:
AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), R.string.cambioemail, R.string.nuevoemail);
myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancelaruserdialog, null)
.show();
break;
case R.id.tvPswdChange:
Intent intent2 = new Intent();
intent2.setClass(getActivity(), UserDialog.class);
intent2.putExtra("PSWD", "PSWD");
startActivity(intent2);
break;
}
}
}
The thing that I've tried to get the LayoutInflater
is getActivity().getLayoutInflater()
, but it doesn't works.
Maybe I figured it out or maybe the logic it's not the correct, but I don't get why I'm doing wrong.
This is the error :
EDIT
Error:(40, 69) 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
Upvotes: 1
Views: 4171
Reputation: 1148
public class ConfiguracionFragment extends Fragment implements View.OnClickListener {
Bundle savedInstanceState;
Context context;
public ConfiguracionFragment(Context context) {
this.context = context;
}
TextView tvEmail,tvPswd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.savedInstanceState =savedInstanceState;
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
tvEmail = (TextView) rootView.findViewById(R.id.tvEmailChange);
tvPswd = (TextView) rootView.findViewById(R.id.tvPswdChange);
tvEmail.setOnClickListener(this);
tvPswd.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvEmailChange:
AlertDialog.Builder myDialog = MyDialog.create(context, getLayoutInflater(savedInstanceState), R.string.cambioemail, R.string.nuevoemail);
myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancelaruserdialog, null)
.show();
break;
case R.id.tvPswdChange:
Intent intent2 = new Intent();
intent2.setClass(getActivity(), UserDialog.class);
intent2.putExtra("PSWD", "PSWD");
startActivity(intent2);
break;
}
}
}
Upvotes: 4