Refresh adapter in a Fragment after DialogFragment dbflow insert - java

I have an activity that has 3 fragments on it with Tabs, one of them is called "TaskFragment".
In my main Activity i only load the fragments.
In TaskFragment i have a RecyclerView that is working fine and is showing the items as intended.
The problem comes, when i insert data using a DialogFragment, because it does insert data (i am using DbFlow ORM), but it does not (of course) refresh the adapter since it is in the TaskFragment fragment inside the DetailMainActivity activity as i said.
I have tried to use onResume() and onPause() in order to refresh the adapter, but they are never called since the activity does not get paused or in onresume for a DialogFragment.
I have tried aswell to use an interface, but it does not work and i have searched all over stackoverflow and google with no luck.
I leave here some of my code for you to understand better:
DetailMainActivity.java
Here in the onClick interface i show the DialogFragment to the user to input the information.
FragmentManager fm = getSupportFragmentManager();
AddSimpleTask sptask = new AddSimpleTask();
sptask.show(fm, "tag");
TaskFragment.java
In this fragment i have my RecyclerView
private void setupRecyclerView() {
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (DetailMainActivity.FAB_Status) {
DetailMainActivity.hideFAB();
DetailMainActivity.FAB_Status = false;
}
return false;
}
});
}
private void setupAdapter() {
adapter = new DetailMainTaskAdapter(simpleTaskList, this);
}
AddSimpleTask
And this is my DialogFragment. I have set a setOnShowListener() in order to avoid the DialogFragment to get dismiss early.
#Override
public void onShow(DialogInterface dialogInterface) {
final AlertDialog dialog =(AlertDialog) getDialog();
if (dialog != null){
Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
Button negativeButton = dialog.getButton(Dialog.BUTTON_NEGATIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mEditTextName.getText().toString().trim().isEmpty() ||
mEditTextContent.getText().toString().trim().isEmpty() ) {
if (mEditTextName.getText().toString().trim().isEmpty()) {
mEditTextName.setError("Can not be empty");
}
if (mEditTextContent.getText().toString().trim().isEmpty()) {
mEditTextContent.setError("Can not be empty");
}
}else {
presenter.beingInsertion(mEditTextName.getText().toString().trim(), mEditTextContent.getText().toString().trim()
, foreignId);
}
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
}
}
If the insert is successfully achieved the onInsertSuccess method is called (i am using MVP)
#Override
public void onInsertSuccess() {
Snackbar.make(getActivity().findViewById(R.id.containerMainDetail), "Actividad agregada", Snackbar.LENGTH_SHORT).show();
dismiss();
}
I have called adapter.notifyDataSetChanged() in many places, and i also tried with a custom interface, but i can not make this work.
Sorry for the long post, but thanks in advance for your help.

There are some errors in your statement but I'll get to that later. notifyDataSetChanged() only notifies the adapter that the underlying list (or array) has changed. The implication is that you first need to requery your database and obtain the new list before calling notifyDataSetChanged() on the adapter else there is no point as the underlying list will still be the same and it will not update the adapter.
The correct way of calling this will be through your custom listener interface and not in the onPause()/onResume() callbacks as there is the possibility that the user does not enter a value and hence you will unnecessarily be querying the database. In your custom listener interface implementation, first update the list with the new data from the DB and then notify the adapter.
Which leads to the error in assumption that onPause()/onResume() callbacks do not happen when your Activity is covered by a DialogFragment - this is incorrect. The moment the activity view is even partially covered, the onPause() callback is triggered.

Related

How to select only one checkbox within 2 recyclerviews

I got 2 recyclerviews on a fragment. Both recyclerviews contain items and checkboxes. Only a single checkbox should be selectable within the two recyclerviews.
So if a checkbox is selected, all other checkboxes should be switched off IN BOTH RECYCLERVIEWS etc..
Here is my current code.
this code means that only one checkbox can currently be selected in each recyclerview.
My two recylcerviewadapters look like this (NOTE: both a quiet identical so I'm only posting one of them):
#Override
public void onBindViewHolder(#NonNull NameViewHolder holder, #SuppressLint("RecyclerView") int position) {
//getting broadcast from 1st recyclerviewadapter
LocalBroadcastManager.getInstance(context).registerReceiver(mNameReceiver, new IntentFilter("checkbox_first_adapter"));
String name = namesArrayList.get(position);
holder.nameView.setText(name);
holder.checkBox.setVisibility(View.VISIBLE);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rowIndex = position;
notifyDataSetChanged();
//If checkbox is checked, send broadcast to 2nd recyclerviewadapter
sendCheckBoxBroadCast();
isClicked = true;
}
});
if (isCheckBoxChecked) {
holder.checkBox.setChecked(false);
}
if (!isClicked) {
if (selectedName != null) {
if (name.equals(selectedName.getName())) {
holder.checkBox.setChecked(true);
sendCheckBoxBroadCast();
}
}
} else {
if (rowIndex == position) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
}
}
public BroadcastReceiver mNameReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
isCheckBoxChecked = intent.getBooleanExtra("isCheckBoxChecked", false);
}
};
private void sendCheckBoxBroadCast() {
Intent intent = new Intent("checkbox_second_adapter");
intent.putExtra("isCheckBoxChecked", true);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
How can I signal the 2nd recyclerview adapter that a checkbox has been selected so that it knows to unselect all checkboxes?
You can go three ways with this:
The first, dirty way is to just pass one adapter to the other and viceversa, and when one has a checkbox event, it callbacks the other one.
The second way would be to implement some sort of interface. You said that both adapters are similar, so it shouldn't be a problem. This interface would have a single method, the callback to update the checkboxes. From the caller activity class, you should be able to manage them both (you could also be able to put them in an array, if you ever need to add more) and get callbacks from either one of the adapters.
The third and perhaps the cleanest way would be to merge the recylcerviews: since a recyclerview can display different kind of views (you just have to fiddle a bit with the onBindViewHolder method) it could be possible (depends on the situation, you haven't provided enough information for this to be possible or entirely ruled out) to use only one adapter class.

How to create a util that can be referenced multiple times in different activities

I have successfully implemented a custom Dialog box that appears when the user tries to leave an activity via a back button or by using onBackPressed(). They can simply cancel the dialog box or continue, and leave the activity. This function has been implemented in multiple activities, however its making my code a lot longer than it needs to be. I wanted to know how to create a util that can be referenced in different activities, without the need for the chunk of code to copy pasted multiple times. Please note that I am retrieving the dialog title and description from string.xml
This is my code:
Dialog customDialog;
Button button_one, button_two;
TextView dialog_title, dialog_description;
customDialog = new Dialog(this);
//Back button will close app
#Override
public void onBackPressed() {
customDialog.setContentView(R.layout.custom_dialog_box);
dialog_title = customDialog.findViewById(R.id.dialog_title);
dialog_title.setText(getString(R.string.leaving_activity_warning_title));
dialog_description = customDialog.findViewById(R.id.dialog_description); dialog_description.setText(getString(R.string.leaving_activity_warning_description));
button_one = customDialog.findViewById(R.id.button_one);
button_one.setText(getString(R.string.cancel));
button_two = customDialog.findViewById(R.id.button_two);
button_two.setText(getString(R.string.leave_anyway));
button_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
button_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
});
Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.show();
}
UPDATE
Created a Java file called "DialogBoxMessage"
DialogBoxMessage Code:
class DialogBoxMessage {
private Dialog customDialog;
private TextView dialog_title, dialog_description;
private Button button_one, button_two;
//Custom Dialog Box Initialization
DialogBoxMessage(Button myButtonOne, TextView myDialogTitle, TextView myDialogDescription, Dialog myCustomDialog) {
customDialog = myCustomDialog;
button_one = myButtonOne;
button_two = myButtonOne;
dialog_title = myDialogTitle;
dialog_description = myDialogDescription;
}
void leaveActivity() {
customDialog.setContentView(R.layout.custom_dialog_box);
dialog_title = customDialog.findViewById(R.id.dialog_title);
dialog_title.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_title));
dialog_description = customDialog.findViewById(R.id.dialog_description);
dialog_description.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_description));
button_one = customDialog.findViewById(R.id.button_one);
button_one.setText(Resources.getSystem().getString(R.string.cancel));
button_two = customDialog.findViewById(R.id.button_two);
button_two.setText(Resources.getSystem().getString(R.string.leave_anyway));
button_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
button_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.show();
}
}
I input the following code in another activity
Other activity code:
//Reusable exit dialog message
DialogBoxMessage dialogBoxMessage;
//Back button will close app
#Override
public void onBackPressed() {
dialogBoxMessage.leaveActivity();
finish();
}
But it doesn't seem to work, I think there are a lot of issues... please help :(
I assume customDialog is a seperate class you wrote - therefore i would suggest you put main information like contentview, title, message or type in the constructor when you initialize ur Dialog.
For your onClick Method I suggest you create an Interface to handle Button Clicks in your
customDialog class.
This could be implemented as a static method in a utilities class. The method would require 'this' as a parameter, which contains the activity context. The method should return the result of the button press. The activity can use this response to determine if finish() should be called or not.
UPDATE
I had suggested a simple static method, but you've gone down the object-oriented route. That's fine.
However, your constructor requires passing in several views, which wouldn't appear to achieve the code efficiency you are after.
Your constructor should just require the Activity context; everything else is encapsulated in your new class.
In each Activity's onBackPressed method you will need to create the object with
dialogBoxMessage = new DialogBoxMessage(this);
before you can call any of that object's methods.

android how to set particular item position for viewpager when onBackPressed()?

// Here is the adapter for viewpager
public void setupViewPager(){
pageAdapter = new PageAdapter(getSupportFragmentManager());
pageAdapter.addFragment(FragmentDesigns.newInstance(), "INDIAFILINGS");
icfo = ICFO.newInstance();
pageAdapter.addFragment(icfo, "iCFO PLATFORM");
viewPager.setAdapter(pageAdapter);
tabLayout.setupWithViewPager(viewPager);
// here code to set particular item in viewpager
int pageRedirect;
pageRedirect=CommonClass.getPageRedirect();
try {
if(pageRedirect!=0){
viewPager.setCurrentItem(1);
}
}catch (Exception e){
e.printStackTrace();
}
}
I am implementing viewpager with two fragments.In second Fragment i have list of data if user selects any item from a list it will redirect to another activity.If user does onbackPress in activity i want to set viewpager second fragment as a current fragment
Note:The issue is when i do onbackPress the application is getting close
#Override
public void onBackPressed() {
if (viewPager.getCurrentItem() != 0) {
viewPager.setCurrentItem(secondViewpagerItemPostion,false);
}else{
super.onBackPressed();
}
}
Store your current position of view pager in sharepref and check it if not null then viewpager.setCurrentItem(shareprefPosition);
override the onBackPressed method in your second activity so that system default behaviour does not happen
#Override
public void onBackPressed() {
Intent firstintent = new Intent(context,Firstactivity.class);
firstintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(firstintent);
}
This will open the first activity, close the second activity and deliver the result in the onNewIntent of the first activity
In your first activity override onNewIntent so that the back press result is found
#Override
protected void onNewIntent(Intent intent) {
yourvp.setCurrentItem(positionyouneed);
}
This should do the trick for you

How to disable a Button in ListView rows connected to ArrayAdapter?

My class ProductAdapter extends ArrayAdapter
on getView i'm inflating rows with 2 buttons in the each row for (+) and (-)
and set anonim OnClickListener for each button , like this :
viewHolder.removeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BigDecimal count = product.getCount().subtract(BigDecimal.ONE);
if (count.signum() < 0) count = BigDecimal.ZERO;
product.setCount(count);
viewHolder.countView.setText(formatValue(count, product.getUnit()));
mListener.onCardClick(v);
}
});
On activity i need to do some AsyncTask when i'm using integer value from each row.
The problem is when AsyncTask executing user still can change product adapter(Buttons are working).
I need to disable them while AsyncTask is working and then reenable after completing.
I was trying to disable ListView with no luck.
Also i was trying to override ArrayAdapter methods isAllEnadled and isEnabled also with no luck.
Interesting problem, you need a State and a place to save this, this State can be used to control the click behavior. You can save this either in the product itself or some other place like a list corresponding to that index
Something like
protected void onPreExecute(Void result) {
product.setFetching(true)
}
protected void onPostExecute(Void result) {
product.setFetching(false)
}
in onClick() you can check same and return like this
if (product.isFetching())
return;
You need to set a callback method for enable/disable buttons. You can use an interface for that.
pseudocode:
public interface ButtonsHandler {
void enableButtons();
void disableButtons();
}
Then you have to implement that interface in your viewHolder
public class CustomViewHolder extends RecyclerView.ViewHolder implements ButtonsHandler {
...
void enableButtons() {
yourButton1.setEnable(true);
yourButton2.setEnable(true);
}
void disableButtons() () {
yourButton1.setEnable(false);
yourButton2.setEnable(false);
}
...
}
Third, when you call the listener from the Holder to start the task, pass the object itself to manage buttons handling.
mListener.onCardClick(View v, ButtonsHandler buttonsHandler);
so the call will be:
mListener.onCardClick(v,viewHolder);
And finally, while your asynktask is working, you can call
buttonsHandler.disableButtons();
...
buttonsHandler.enableButtons();

OnCancelListener is not called in DialogFragment

I have a simple AlertDialog that displays a list of some items and upon clicking one of them, the clicked item is passed back to the enclosing Activity. I also want to perform some default handling when the user cancels the dialog (using the back button) - more specifically, I want to pass an empty string to the activity in such case.
However, if I put the dialog in a DialogFragment (from the compatibility package), the OnCancelListener is not called when I close the dialog with the back button. What am I doing wrong?
public class SelectItemDialog extends DialogFragment {
public interface Callback {
void onItemSelected(String string);
}
private static final String ARG_ITEMS = "items";
private Callback callback;
public static SelectItemDialog fromItems(Collection<String> items) {
SelectItemDialog fragment = new SelectItemDialog();
fragment.setArguments(newArguments(items));
return fragment;
}
private static Bundle newArguments(Collection<String> items) {
Bundle arguments = new Bundle();
arguments.putStringArray(ARG_ITEMS, items.toArray(new String[items.size()]));
return arguments;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callback = (Callback) activity;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String[] items = getArguments().getStringArray(ARG_ITEMS);
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_select_email_title)
.setItems(items, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
callback.onItemSelected(items[which]);
}
})
.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// this code is not executed
callback.onItemSelected("");
throw new RuntimeException("dialog cancelled");
}
})
.create();
}
}
It might have to do with the fact that there is no explicit call to cancel() from your code.
The OnCancelListener documentation says:
This will only be called when the dialog is canceled
Which probably needs an explicit cancel() call.
Either make a positive/negative button with a OnClickListener that calls DialogInterface#cancel() or use a OnDismissListener() with an extra check to see if a list item was clicked.
Also, to listen for a back keypress and cancel the dialog, you can set up an OnKeyListener, like outlined in this SO answer
Also, once you have the Dialog set up, it would also be a good idea to use Dialog#setCanceledOnTouchOutside() in case the the user taps outside the Dialog.
Edit: The below part is the easy way to handle cancel events in a DialogFragment.
Since you are using a DialogFragment, this class has a very handy method, DialogFragment#onCancel() which gets called when the DialogFragment is cancelled. Do your logic in there.
DialogFragments are more complex, with a slightly different lifecycle than normal dialogs. Therefore, first check the documentation if you have a certain Dialog-based approach that you are trying to port to a DialogFragment, some methods may exist that allow your new implementation to function properly!
If you are using DialogFragment and want to listen back button then use this -
this.getDialog().setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (****) {
your logic
}
return true;
}
return false;
}
});
Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself.
To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).
public class SelectItemDialog extends DialogFragment {
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
//your code hear
dialog.cancel();
}
}
And you should remove .setOnCancelListener()
Actually if you want to use DialogFragment, you can never add OnCancelListener or OnDismissListener to it, since the Dialog Fragment owns callbacks to these methods!
You have 3 options here:
1- go with regular dialogs.2- set your dialog fragment to cancellable(false) and add a cancel button to the dialog.3- check #Nikhil Pingle answer.
this is from the documentation of the Dialog Fragment
* <p><em>Note: DialogFragment own the {#link Dialog#setOnCancelListener
* Dialog.setOnCancelListener} and {#link Dialog#setOnDismissListener
* Dialog.setOnDismissListener} callbacks. You must not set them yourself.</em>
* To find out about these events, override {#link #onCancel(DialogInterface)}
* and {#link #onDismiss(DialogInterface)}.</p>
Cancel Listener or Dismiss listener in DialogFragment can achieve by onDismiss
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
newFragment.onDismiss(new DialogInterface(){
#Override
public void cancel() {
// TODO Auto-generated method stub
}
#Override
public void dismiss() {
// TODO Auto-generated method stub
}
});

Categories

Resources