Create Custom AlertDialog Builder and Change Font of Title & Message - java

I have created a Custom AlertDialog Builder and I need to change font of title and message in alertdialog but am not able to achieve this by following way.
CustomAlertDialogBuilder.java :
public class CustomAlertDialogBuilder extends AlertDialog.Builder {
public CustomAlertDialogBuilder(Context context) {
super(context);
TextView title = (TextView) create().getWindow().findViewById(R.id.alertTitle);
TextView message = (TextView) create().getWindow().findViewById(android.R.id.message);
myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/my_font.ttf");
title.setTypeface(myTypeface);
message.setTypeface(myTypeface);
}
}
infact the TextView's are null. How do I define TextViews? I'm a beginner, Please help me to change font of alertdialog with create custom alertdialog.

I use custom dialogs quite often so I use DialogFragment. Note this dialog has an "Ok" and "Cancel" buttons. You can remove the buttons if you do not need them.
You need to create an XML Layout for the Custom DialogFragment "fragment_submit_cancel_dialog". The ability to create your own design gives you a great deal of flexibility in the appearance of your dialog.
In the Activity you call the DialogFragment you will need to add this:
implements OkCancelDialogFragment.OkCancelDialogListener{
and add the listener method:
#Override
public void onFinishOkCancelDialog(boolean submit) {
if(submit){
// Do what you need here
}
}
Call the DialogFragment like this:
private void startOkDialog(){
String title = "What ever you want as a Title";
String mess = "Your Message!";
OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess);
show(getFragmentManager(), "OkDialogFragment");
}
Now the code for the Custom Dialog Fragment:
public class OkCancelDialogFragment extends DialogFragment {
private static final String ARG_TITLE = "title";
private static final String ARG_MESSAGE = "message";
Context context = null;
private String title;
private String message;
private boolean submitData = false;
private OkCancelDialogListener mListener;
public OkCancelDialogFragment() {
}
public static OkCancelDialogFragment newInstance(String title, String message) {
OkCancelDialogFragment fragment = new OkCancelDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString(ARG_TITLE);
message = getArguments().getString(ARG_MESSAGE);
}
}
#Override
public Dialog onCreateDialog(Bundle saveIntsanceState){
context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_submit_cancel_dialog, null, false);
final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle);
final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage);
titleView.setText(title);
messView.setText(message);
builder.setView(rootView)
.setPositiveButton(R.string.button_Ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
submitData = true;
//The onDetach will call the Listener! Just in case the user taps the back button
}
})
.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
submitData = false;
}
});
return builder.create();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
if(mListener == null) mListener = (OkCancelDialogListener) context;
}
catch (Exception ex){
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener.onFinishOkCancelDialog(submitData);
mListener = null;
}
public interface OkCancelDialogListener {
void onFinishOkCancelDialog(boolean submit);
}
}

Related

Listener for custom dialog null

I'm getting a NullPointerException for my listener in my class, even though everything should be working fine. I followed Coding in Flow's tutorial for the custom dialog, rewatched it a few times and checked source code and I didn't miss anything. here's the code
Fragment the dialog is called from
public class FragmentMain extends Fragment implements CustomDialog.DialogListener {
.
.
.
private int timeLimit = 0;
private Button dialogOpen;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
.
.
.
dialogOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogDashboard dialogDashboard = new DialogDashboard();
dialogDashboard.show(getActivity().getSupportFragmentManager(), "notif_dialog");
}
}); //dialog called here
return view;
}
//getting text from spinner in dialog
#Override
public void applyText(String time) {
switch (time) {
case "30 Minutes":
timeLimit = 30;
case "1 Hour":
timeLimit = 60;
case "2 Hours":
timeLimit = 120;
case "4 Hours":
timeLimit = 240;
case "5 Hours":
timeLimit = 300;
}
}
}
Dialog Code:
public class CustomDialog extends AppCompatDialogFragment {
private Button confirmButton;
private Spinner spinner;
private DialogListener listener;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
R.style.NotificationAlertDialog);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.custom_dialog_notif, null);
confirmButton = view.findViewById(R.id.dialogButton);
spinner = view.findViewById(R.id.dialogSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String time = spinner.getSelectedItem().toString();
listener.applyText(time); // says listener is null when I just do e.printStackTrace();
dismiss();
}
});
builder.setView(view)
.setCancelable(true);
return builder.create();
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listener = (DialogListener) context;
} catch (Exception e) {
e.printStackTrace();
throw new ClassCastException(context.toString() +
"must implement dialog listener"); // this says if the listener is null, but even if it's implemented it returns null...
}
}
public interface DialogListener {
void applyText(String time);
}
}
Root cause
You got NPE because onAttach() callback is called when the fragment is attached to its host/activity. The context param is the activity itseft, not the FragmentMain.
Solution
Step 1: When you click on the open dialog button in FragmentMain.
CustomDialog dialogDashboard = new CustomDialog();
dialogDashboard.show(getChildFragmentManager(), "notif_dialog");
Step 2: Modify the code in CustomDialog
Remove this code
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listener = (DialogListener) context;
} catch (Exception e) {
e.printStackTrace();
throw new ClassCastException(context.toString() +
"must implement dialog listener");
}
}
When users click on the confirm button.
String time = spinner.getSelectedItem().toString();
Fragment fragment = getParentFragment();
if (fragment instanceof CustomDialog.DialogListener) {
listener = (DialogListener) fragment;
listener.applyText(time);
}
dismiss();

AppCompatDialogFragment listener not passing values when I do

The line 'listener2.applyTexts2(infor);'
gives me an error that I pass a null object reference on applyTexts2. I debugged this line and 'infor' has all values passed to it normally, yet it still says that it is null. What did I do wrong?
(I am calling this from my MainActivity from a button and the popup to insert the info is fine, everything looks good, yet it says about null when I have no null in the data. I have another listener in my MainActivity and it works fine. Does the first one somehow mess the second one? They are completely separate classes)
public class StaffDialog extends AppCompatDialogFragment{
private StaffDialogListener listener2;
private EditText FirstName;
private EditText LastName;
private EditText Phone;
private EditText Linkedin;
private EditText Tran;
private String[] infor = new String[5];
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog2,null);
builder.setView(view)
.setTitle("Enter this person's information")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
infor[0]=FirstName.getText().toString();
infor[1]=LastName.getText().toString();
infor[2]=Phone.getText().toString();
infor[3]=Linkedin.getText().toString();
infor[4]=Tran.getText().toString();
listener2.applyTexts2(infor);
}
});
FirstName = view.findViewById(R.id.name);
LastName = view.findViewById(R.id.lastname);
Phone = view.findViewById(R.id.phone);
Linkedin = view.findViewById(R.id.linkedin);
Tran = view.findViewById(R.id.tran);
return builder.create();
}
#Override
public void onAttach(Context context){
super.onAttach(context);
try{
listener2 = (StaffDialogListener) context;
}catch(Exception e){
e.printStackTrace();
}
}
public interface StaffDialogListener{
void applyTexts2(String[] info);
}
}```

Dynamically add Views in Dialog

I am trying to make Dialog that will consist 2x EditText and 1x Buttons. By clicking additional button you can add another 2x EditText and 1x Buttons. This 1x Button provide deleting this added pair. When i try to use button that should add another Views it's working properly. But button for deleting the View is working only for the first pairs. How can i do this with android:onClick, because i was trying buy it crashed.
Here is my code of Dialog class:
public class ExampleDialog extends AppCompatDialogFragment {
private EditText editTextUsername;
private EditText editTextPassword;
private ExampleDialogListener listener;
private LinearLayout parentLinearLayout;
private Context mContext;
Button dodaj,usun;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_template, null);
builder.setView(view)
.setTitle("Login")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
listener.applyTexts(username, password);
}
});
editTextUsername = view.findViewById(R.id.edit_username);
editTextPassword = view.findViewById(R.id.edit_password);
parentLinearLayout = (LinearLayout) view.findViewById(R.id.parent_linear_layout);
dodaj = view.findViewById(R.id.add_field_button);
usun = view.findViewById(R.id.delete_button);
dodaj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
});
usun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
parentLinearLayout.removeView((View) v.getParent());
usun = v.findViewById(R.id.delete_button);
}
});
return builder.create();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (ExampleDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement ExampleDialogListener");
}
}
public void contexta(Context context)
{
this.mContext = context;
}
public interface ExampleDialogListener {
void applyTexts(String username, String password);
}
}
And there is the conception of Dialog box on the picture below:
Picture
This issue may be due to view reference. Check whether v.getParent() is the view you want to delete or not.
For testing you can use "removeViewAt(int index)" method. Pass an index and check whether view is deleted at index or not.

android.view.WindowManager$BadTokenException error while creating dialog box

I am trying to create a Dialog Box from an Non activity Class.
This is my Code
public static void ShowDialogBox(final Context con, final Listener list) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(con);
dlgAlert.setMessage("TEXT");
dlgAlert.setTitle("TEXT");
dlgAlert.setPositiveButton("TEXT"),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show(); // THIS LINE GIVES ME AN ERROR
}
This is the error which I am getting
android.view.WindowManager$BadTokenException: at
android.view.ViewRootImpl.setView (ViewRootImpl.java:574) at
android.view.WindowManagerGlobal.addView
(WindowManagerGlobal.java:282) at
android.view.WindowManagerImpl.addView (WindowManagerImpl.java:85)
at android.app.Dialog.show (Dialog.java:298) at
PACKAGE NAME AND CLASS
PACKAGE NAME AND CLASS at
PACKAGE NAME AND CLASS
PACKAGE NAME AND CLASS at
PACKAGE NAME AND CLASS.onBackPressed
(Class.java:95) at android.app.Activity.onKeyUp
(Activity.java:2465) at android.view.KeyEvent.dispatch
(KeyEvent.java:2646) at android.app.Activity.dispatchKeyEvent
(Activity.java:2716) at
android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent
(WindowCallbackWrapper.java:50) at
android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent
(AppCompatDelegateImplBase.java:224) at
com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent
(PhoneWindow.java:2280) at
android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent
(ViewRootImpl.java:4038) at
android.view.ViewRootImpl$ViewPostImeInputStage.onProcess
(ViewRootImpl.java:4000) at
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3562)
at android.view.ViewRootImpl$InputStage.onDeliverToNext
(ViewRootImpl.java:3615) at
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3581)
at android.view.ViewRootImpl$AsyncInputStage.forward
(ViewRootImpl.java:3698) at
android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3589)
at android.view.ViewRootImpl$AsyncInputStage.apply
(ViewRootImpl.java:3755) at
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3562)
at android.view.ViewRootImpl$InputStage.onDeliverToNext
(ViewRootImpl.java:3615) at
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3581)
at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3589)
at android.view.ViewRootImpl$InputStage.deliver
(ViewRootImpl.java:3562) at
android.view.ViewRootImpl$InputStage.onDeliverToNext
(ViewRootImpl.java:3615) at
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3581)
at android.view.ViewRootImpl$AsyncInputStage.forward
(ViewRootImpl.java:3731) at
android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent
(ViewRootImpl.java:3892) at
android.view.inputmethod.InputMethodManager$PendingEvent.run
(InputMethodManager.java:2208) at
android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback
(InputMethodManager.java:1849) at
android.view.inputmethod.InputMethodManager.finishedInputEvent
(InputMethodManager.java:1840) at
android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished
(InputMethodManager.java:2185) at
android.view.InputEventSender.dispatchInputEventFinished
(InputEventSender.java:141) at
android.os.MessageQueue.nativePollOnce (Native Method) at
android.os.MessageQueue.next (MessageQueue.java:143) at
android.os.Looper.loop (Looper.java:122) at
android.app.ActivityThread.main (ActivityThread.java:5254) at
java.lang.reflect.Method.invoke (Native Method) at
java.lang.reflect.Method.invoke (Method.java:372) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:902) at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:697)
Here is the scenario of the user
Activity A -->Opens Activity B-->User presses Back button in Activity B--> On Back button pressed a listener is sent to Activity A --> And then the Dialog Box shown is called.
The issue you are having with the attempt to build your AlertDialog in a separate class is you are passing the AlertDialog the Context of your Activity. You get the error because the AlertDialog requires the WindowManager from the Activity which has the layout--not the Context. This is because Activit extends Context... not the other way around.
In order to make your code work you need to provide the AlertDialog.Builder access to the Activity. So change your code to something like this:
public class TestDialog {
private static final String TAG = TestDialog.class.getSimpleName();
Activity mActivity;
public TestDialog(Activity activity){
mActivity = activity;
}
public void showDialog(){
AlertDialog.Builder b = new AlertDialog.Builder(mActivity);
b.setTitle("Title");
b.setMessage("message");
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "showDialog : onClick");
}
});
b.create().show();
}
}
Now you can call the AlertDialog from an Activity lets say in this case MainActivity like this:
TestDialog testDialog = new TestDialog(MainActivity.this);
testDialog.showDialog();
I have not tried this from a Fragment, so I do not know whether this works from a Fragment or whether you will continue to have issues with certain devices. For those reasons, I (and Google!) still strongly suggest that you use the DialogFragment instead, because is has been especially designed for this scenario. Take a look at the Google Docs.:
https://developer.android.com/guide/topics/ui/dialogs
I generally prefer using DialogFragment instead of what you attempted in order to cut down on repetition. Here is an example of a DialogFragment with a custom layout which I have called R.layout.fragment_alert_dialog:
public class AlertDialogFragment extends DialogFragment {
private static final String ARG_TITLE = "title";
private static final String ARG_MESSAGE = "message";
private String title;
private String message;
boolean endSuccess = false;
private AlertFinishedDialogListener mListener;
public AlertDialogFragment() {
}
public static AlertDialogFragment newInstance(String title, String message) {
AlertDialogFragment fragment = new AlertDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString(ARG_TITLE);
message = getArguments().getString(ARG_MESSAGE);
}
}
#Override
public Dialog onCreateDialog(Bundle saveIntsanceState){
final Context context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_alert_dialog, null, false);
final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle);
final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage);
titleView.setText(title);
messView.setText(message);
builder.setView(rootView)
// .setTitle(title)
.setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
endSuccess = true;
if(mListener == null) mListener = (AlertFinishedDialogListener) context;
mListener.onAlertFinishedDialog();
}
});
return builder.create();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
if(mListener == null) mListener = (AlertFinishedDialogListener) context;
}
catch (Exception ex){
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface AlertFinishedDialogListener {
void onAlertFinishedDialog();
}
}
It incorporates a Listener just in case you need to be notified when the DialogFragment is completed.
First you need to implement the callback:
implements AlertDialogFragment.AlertFinishedDialogListener{
In order to call the AlertDialogFragment you do this from your Activity (also Fragment if necessary).
private void startAlertDialogFragment(String title, String mess){
AlertDialogFragment alert = AlertDialogFragment.newInstance(title, mess);
alert.show(getFragmentManager(), "alertDialogFragment132");
}
#Override
public void onAlertFinishedDialog() {
Log.e(TAG, "onAlertFinishedDialog");
}
Problem
You can show dialogs from activity context only. except TYPE_SYSTEM_ALERT or TYPE_APPLICATION_OVERLAY, which is not recommended if your app does not show emergency notifications to user.
Solution
If you have activity context available then you can show dialog from any class like service, broadcast receiver, or even any class you imagine.
Here is my workaround that can show dialog from any class like i said.
Here is a snippet what i do to show dialog from any class. (Could it
be more simpler!)
import android.app.Dialog;
import android.content.DialogInterface;
public class SampleClass {
void anyMethod() {
Dialog dialog = ApplicationContext.getInstance().showDialog("title", "yourMessage", "Cancel", "Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO: handle button 1 clicked
}
}, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO: handle button 2 clicked
}
});
}
}
Now you will implement to make this work.
1. make application class which you will register in android manifest application tag
<application
android:name=".ApplicationContext"
...
>
...
</application>
2. In this application class you will hold live activity object. that will further useful for showing dialog.
ApplicationContext.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
public class ApplicationContext extends Application {
private static ApplicationContext mInstance;
private Activity liveActivity;
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
#Override
public void onTerminate() {
super.onTerminate();
mInstance = null;
}
public static synchronized ApplicationContext getInstance() {
return mInstance;
}
public Activity getLiveActivity() {
return liveActivity;
}
public void setLiveActivity(Activity liveActivity) {
this.liveActivity = liveActivity;
}
/*
* Show Dialog with Title, Message, Button1, Button2 with Button1 and Button2 Listener
*/
public AlertDialog showDialog(String title, String msg,
String btn1, String btn2,
DialogInterface.OnClickListener listener1,
DialogInterface.OnClickListener listener2) {
if (liveActivity == null) return null;
AlertDialog.Builder builder = new AlertDialog.Builder(liveActivity)
.setTitle(title)
.setMessage(msg)
.setCancelable(false)
.setPositiveButton(btn1, listener1);
if (btn2 != null)
builder.setNegativeButton(btn2, listener2);
AlertDialog alert = builder.create();
alert.show();
return alert;
}
}
Just one more step
3. You will extend all your activity by this base activity class (You can edit your base activity if you already have one.)
import android.support.v7.app.AppCompatActivity;
public class BaseActivity extends AppCompatActivity {
#Override
protected void onResume() {
super.onResume();
ApplicationContext.getInstance().setLiveActivity(this);
}
#Override
protected void onPause() {
super.onPause();
ApplicationContext.getInstance().setLiveActivity(null);
}
}
Here you go !!!

How to get response from interface method in adapter in android

So here I am calling the interface method from the adapter class. I want to update the List based on the user's input. How would I achieve it?
Adapter class:
public class ToDoListAdaptor extends SectionRecyclerViewAdapter < SectionHeader, Action, SectionViewHolder, ChildViewHolder > {
#Override
public void onBindChildViewHolder(final ChildViewHolder holder, final int sectionPosition, final int childPosition, final Action child) {
Context mainActivityContext = Constants.getContext();
if (action_id.equals("pain")) {
if (mainActivityContext != null && mainActivityContext instanceof MainActivity) {
interfaceAdapter = ((HealthVitalsFunction) mainActivityContext);
boolean result = interfaceAdapter.openPainRecordDialog(context, dbHelper, action_id, action_cat_id, action_plan_id, action_name);
if (result)
update(sectionHeaderList, childPosition);
}
}
}
}
The problem is that I am not able to call update() when user done with input.
edit:
#Override
public boolean openPainRecordDialog(final Context context, final DbHelper dbHelper, final String action_id, final String action_cat_id, final String action_plan_id, final String action_name) {
Constants.painData=false;
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
final View mView = layoutInflaterAndroid.inflate(R.layout.pain_record, null);
final AlertDialog dialog = new AlertDialog.Builder(context)
.setView(mView)
.setTitle("")
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null)
.setCancelable(false)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(final DialogInterface dialog) {
Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Do something
if (dialog != null && ((AlertDialog) dialog).isShowing()) {
dialog.dismiss();
}
Constants.painData = true;
}
});
Button negativeButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Do something
if (dialog != null && ((AlertDialog) dialog).isShowing()) {
dialog.dismiss();
}
Constants.painData = false;
}
});
}
});
// show it
dialog.show();
return Constants.painData;
}
I want to update the list based on the user's input.
If you are using a custom dialog, provide custom interface callback which defines onSuccess() method and implement it in your calling activity, which can be used to update the recyclerView. cheers :)

Categories