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;
}
}
}