The ListView on the swipe tab is not updated unless you restart

In my application, it has two tabs, one of which is a reminder, and the other is a completed task.

enter image description here

When the toggle button is pressed, I want it to move the list to the Finished task.

The idea is this:

  • Get verified row id from sqlite
  • Get the data based on the identifier from the reminder table and insert it into the completed table.
  • Call the return method in the Completed tab.

But when I pressed the switch button and navigated to "Finished", it is still empty. After I exit the application and swipe the tab, only data will be displayed.

How can I make data right away on the Finished tab when it scrolls, rather than exit the application and open it again? Thanks

AllAdapter (Reminder)

holder.toggle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (((ToggleButton)v).isChecked()) { int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. search.get(getPosition).setSelected(((ToggleButton) v).isChecked()); int id= search.get(getPosition).getID(); mdb = new MyDatabaseHelper(v.getContext()); database = mdb.getReadableDatabase(); Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE ID = ? ", new String[]{id+""}, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String allTask = cursor.getString(cursor.getColumnIndex("Title")); String name = cursor.getString(cursor.getColumnIndex("Name")); String allTime = cursor.getString(cursor.getColumnIndex("Time")); String allDate = cursor.getString(cursor.getColumnIndex("Date")); insertDataToCompleteTab(id,name,allTask,allTime,allDate); // insert to another table } } } else { int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. search.get(getPosition).setSelected(((ToggleButton) v).isChecked()); } } }); 

CompletedTask

 retrieveList(name); public void retrieveList(String name) { Toast.makeText(getActivity(),name,Toast.LENGTH_SHORT).show(); search.clear(); database = mdb.getReadableDatabase(); Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE_TASKCOMPLETED + " WHERE Name = ? ", new String[]{name}, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { int iD = cursor.getInt(cursor.getColumnIndex("ID")); String allTask = cursor.getString(cursor.getColumnIndex("Title")); String allTime = cursor.getString(cursor.getColumnIndex("Time")); String allDate = cursor.getString(cursor.getColumnIndex("Date")); if (adapter != null) { adapter.add(iD, allTask, allTime, allDate); listview.setAdapter(adapter); adapter.getCount(); // check(); } } } else { } } 

Alladapter

http://pastebin.com/qbLDtf4v

Completed tab

http://pastebin.com/WCCbZ0h4

CompleteAdapter

http://pastebin.com/QdbuTQKm

+5
source share
5 answers

From Android documentation for the fragment life cycle

The displayed fragments of the onResume() or onPause() will be called only when the onResume() or onPause() actions are onResume() . They are closely related to activities.

Because of this, you will not be able to refresh your view when the tab changes.

To solve this problem, I found one solution in setUserVisibleHint in my application, since we had the same requirement. Viewpager calls the following function of your fragment and implements it in the code of the fragment

 @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); // Make sure that we are currently visible if (this.isVisible()) { // If we are becoming invisible, then... if (!isVisibleToUser) { Log.d("TAG", "Stop animation and audio you are not visible"); } } } 

That's what the documentation says

Set a hint to the system about whether this user interface fragment is visible to the user. This prompt is set to true by default and is persistent through saving and restoring an instance of an instance of an instance. An application can set this to false to indicate that the fragment's user interface scrolls out of visibility or is not otherwise displayed directly to the user. This can be used by the system to prioritize operations, such as updating the fragment life cycle or ordering the bootloader. This method can be called out of the life cycle fragment. and therefore has no guarantee of order in relation to calls to the fragment life cycle method.

ViewPager calls this on its child fragment. For more information, ViewPager also calls setMenuVisibility , which you can use if you encounter a problem when displaying or deleting a menu. see the setMenuVisiblity documnet link, which states that

Set a hint about whether this snippet menu should be visible. This is useful if you know that a fragment has been placed in your view of the hierarchy so that the user cannot see it at the moment, so any menu items that he should also not show.

+3
source

By default, the closest tabs inside the ViewPager . This happened because the data was uploaded to the second tab before you made any switches using the toggle button. To fix the problem, you need to update the data on the completed tab in case of data changes.

This can be achieved in several ways:

  • The first fragment should send an event for each change, and the second should subscribe to this event and manually update the data

  • Use the downloader and content provider (or just custom uri settings). Loader subscribes to any uri changes, and your repository / tao database assistants notify uri of any changes (when the input / update / delete methods were called)

context.getContentResolver().notifyChange(uri);

Loader for your objects.

 abstract class CachedLoader<T> extends AsyncTaskLoader<T> { @Nullable private T cachedData; CachedLoader(Context context) { super(context); } void registerUri(Uri... observerUris) { for (Uri uri:observerUris) { getContext().getContentResolver().registerContentObserver(uri, true, createContentObserver()); } } ContentObserver createContentObserver() { return new ForceLoadContentObserver(); } @Override public void deliverResult(T data) { super.deliverResult(data); this.cachedData = data; } @Override public void onContentChanged() { super.onContentChanged(); cachedData = null; } @Override protected void onStartLoading() { if (!takeContentChanged() && cachedData != null) { deliverResult(cachedData); return; } forceLoad(); } @Override protected void onReset() { super.onReset(); cachedData = null; } } 

uri call register to subscribe to uri changes.

Or use CursorLoader because you work directly with cursors. As I see in the updated question

I prefer the second approach, but the first one is simpler

+4
source

So, I just put below code in my fragment of a completed task

  @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if(isVisibleToUser){ retrieveList(name);//call the method to be refreshed } else{ //no } } 

Getting help from @Swapnil and response from fooobar.com/questions/683149 / ...

+3
source

put below code on onResumse snippet like this

  @Override public void onResume() { super.onResume(); retrieveList(name); } 
+3
source

What you can do is create an interface line by line

 public interface ReminderCompletedListener { void reminderCompleted(); } 

In CompletedTask create an instance of this listener and register it in your activity.

 ((MyActivity) getActivity()).setReminderCompletedListener(new ReminderCompletedListener() { @Override public void reminderCompleted() { retrieveList(name); } }); 

In your activity, create a method that you can call from your switch to switch between clicks

 ((MyActivity) getActivity()).reminderCompleted(); 

This will reload the list of completed lists when completing a task from the Reminders tab. One of the main considerations that should be made in this decision is that it directly relates your fragments to your activity.

In addition, you can use things like a bus (eventbus / otto), possibly rxjava, you can explore them if you want, but the vanilla solution I gave should do the job perfectly.

+3
source

Source: https://habr.com/ru/post/1260267/


All Articles