0

My app has two tables that are able to be inserted to: a Contacts table and a Messages table. Each table has a ListView Adapter linked to it, and the idea is that when a new entry is inserted into either table, the adapter will update with notifyDataSetChanged() and will show up in the ListView. I've gotten it to work with the Contacts code, but when I try the same thing with the Messages code (shown below), it inserts the entry into the table, but doesn't update the ListView. The ListView only updates when I uninstall the app or upgrade the SQLite db. I have notifyDataSetChanged() implemented and tried updating the adapter and cursor from onResume(), but it didn't make any difference. I've also checked these sources, among others, and no luck:

Android - ListView not being updated after adding to SQLite database unless app is restarted

Android ListView not refreshing after notifyDataSetChanged

onPostExecute() that gets the data and updates adapter:

// Saves all data received into a cursor, which is then saved to the 
// List<Message> object
List<Message> arrayOfMessages = dbHelper.getAllMessages();

dbHelper.close();

final MessagesAdapter adapter = new MessagesAdapter(getContext(), arrayOfMessages);

ListView listView = (ListView) getActivity().findViewById(R.id.messages_listview);
listView.setAdapter(adapter);

fragmentsPagerAdapter.notifyDataSetChanged();

swipeRefreshLayout.setRefreshing(false);

Message Adapter Class

public class MessagesAdapter extends ArrayAdapter<Message> {

    public MessagesAdapter(Context context, List<Message> messages) {
        super(context, 0, messages);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        Message message = getItem(position);

       // Get rest of convertView...
    }
}   

Below is the Activity that handles the tab layout and ViewPager:

public class WhoopNavigationActivity extends AppCompatActivity
    implements FeedFragment.OnFragmentInteractionListener, NetworkFragment.OnFragmentInteractionListener,
    ProfileFragment.OnFragmentInteractionListener, NetworkNewModal.NetworkNewModalListener {

    SaveStateParams saveStateParams = new SaveStateParams();
    ServerPost serverPost = new ServerPost();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_whoop_navigation);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final FragmentsPagerAdapter fragmentsPagerAdapter = new FragmentsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        final ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(fragmentsPagerAdapter);

        // View of activity
        View view = findViewById(android.R.id.content);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        tabLayout.setupWithViewPager(mViewPager);
    }

    public static class FragmentsPagerAdapter extends FragmentStatePagerAdapter {

        // Constructor
        public FragmentsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch(position) {
                case 0:
                    return FeedFragment.newInstance();
                case 1:
                    return NetworkFragment.newInstance();
                case 2:
                    return ProfileFragment.newInstance();
            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        public int getItemPosition(Object item) {
            // Causes adapter to reload all Fragments when notifyDataSetChanged is called
            return POSITION_NONE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Feed";
                case 1:
                    return "Network";
                case 2:
                    return "Profile";
            }
            return null;
        }

    }
}
Community
  • 1
  • 1
jexx04
  • 3
  • 2
  • Check this Answer http://stackoverflow.com/questions/14503006/android-listview-not-refreshing-after-notifydatasetchanged?lq=1 – Ahmad.Aslam90 May 31 '16 at 19:04
  • @Ahmad.Aslam90: Thanks for the answer! I've looked at that, and I've already tried both updating the cursor and adapter in `onResume()` and replacing `List arrayOfMessages = dbHelper.getAllMessages();` with `arrayOfMessages.addAll(dbHelper.getAllMessages)` (along with the other necessary changes) and no luck. – jexx04 May 31 '16 at 19:23
  • You could try a CursorAdapter instead of an ArrayAdapter since you are using SQLite – OneCricketeer Jun 01 '16 at 03:11
  • I'll look into a CursorAdapter, thanks! – jexx04 Jun 02 '16 at 05:15

3 Answers3

0

return convertView in public getView()

check this link also

ViewPager PagerAdapter not updating the View

Community
  • 1
  • 1
Ahmad.Aslam90
  • 74
  • 1
  • 10
  • I should have stated that the code is pretty truncated so the viewer doesn't have to scroll forever (the `getView()` method is pretty long), and I do return convertView. I also checked out that link, and it's very good info, but I still can't get my code to work with it. – jexx04 May 31 '16 at 21:21
0

Actually you are applying notifyDataSetChanged in pager adapter rather than in list view adapter, at least that is what it looks, So apply notify DataSetChanged in listView adapter. If you have defined list view in some fragment than make a public method in the fragment to update the data set (where you will call the notifyDataSetChanged for the adapter there) and store the all instances of fragment used in tab layout in some sparsed array so you can call the corresponding method of the fragment using the instance from the main activity of yours. If I am wrong them please comment then I will update my answer as I have already implement something like this and my code is working great..

AndroidEngineX
  • 975
  • 1
  • 9
  • 22
0

So I appreciate everyone's answer, but after I was finally able to get back to this error, it turns out that there was a problem with my GCM implementation on the server. Once I commented out the GCM portion of the Android code, the list updated itself as expected. I feel a little silly. Thanks anyway y'all!

jexx04
  • 3
  • 2