0

i have 3 class. MainActivity,OneFragment,TwoFragment, I have listview which i fill using by Sqlite on OneFragment.And I add record from TwoFragment (with CreateData function using by my SqlHelper class) but new record dont showing in the listview.

How can i solve this?

If you want to see codes i can upload.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

I highly recommend you to use RecyclerView instead of ListView (as Google says:

The RecyclerView widget is a more advanced and flexible version of ListView.

), but Both of them you need some refresh data may be like this on your OneFragment:

@Override
public void onResume() {
    super.onResume();
    items.clear();
    items = dbHelper.getItems(); // reload the items from database
    adapter.notifyDataSetChanged();
}

this question may help you with your problem Android ListView not refreshing after notifyDataSetChanged

if you do not understand send code

Mukhtar Bimurat
  • 356
  • 2
  • 18
  • The above solution assumes an array and would not work if the source of the ListView was a Cursor, as Cursors do not have a `clear` method. Thus if using a cursor simply omit `items.clear()`. For cursors you can also use `adapter.swapCursor(items);` instead of `adapter.notifyDataSetChanged();`. I use the latter simply because it's more descriptive. – MikeT Jun 23 '17 at 21:42
  • Thank you for answers Mukhtar and MikeT .Now i do your solition ways and i can do it with swapCursor but it works when i reopen application . it dont work tab change i try onPause,onResume but results are same. – Statistician Jun 24 '17 at 00:30
  • I've never used fragments as yet. However this might be of use [What event is triggered when a tab fragment is selected](https://stackoverflow.com/questions/23970301/what-event-is-triggered-when-a-tab-fragment-is-selected) or perhaps this [ViewPager call setUserVisibleHint after first Fragment is loaded](https://stackoverflow.com/questions/25667657/viewpager-call-setuservisiblehint-after-first-fragment-is-loaded/25822814#25822814) – MikeT Jun 24 '17 at 01:11