I cannot figure out how to correctly use notifyDataSetChanged within my app

你离开我真会死。 提交于 2019-12-06 05:23:23

It looks like the main issue here is getting the data added event down to the Fragment where the adapter lives.

You can just call a public method in MainActivity from your listener, that uses a reference to the Fragment to call a public method that will call notifyDataSetChanged() on the adapter.

First, use an instantiateItem() override in your FragmentPagerAdapter to get a valid reference to your ChatFragment:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    public ChatFragment chatFragment;

    //...

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new ChatFragment();
            case 1:
                ...
            default:
                return null;
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
      if (position == 0) {
        chatFragment = (ChatFragment) createdFragment;
      }
      return createdFragment;
    }
}

Then, define the method in MainActivity that will relay the data changed event:

public void dataChanged() {
    //only update if the user is currently on the ChatFragment
    if (mViewPager.getCurrentItem() == 0 && mPagerAdapter.chatFragment != null) {
        mPagerAdapter.chatFragment.listViewDataChanged();
    }
}

Then define the listViewDataChanged() method in your ChatFragment, and make sure that the adapter is a member variable of the Fragment class:

public void listViewDataChanged() {
    simpleCursorAdapter.notifyDataSetChanged();
}

//Class member variable:    
SimpleCursorAdapter simpleCursorAdapter;

public void displayChats(){
    DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
    Cursor chatsCursor = databaseHelper.getChatsCursor();
    String[] fromColumns = {chatInfo, chatContent};
    int[] toViews = {R.id.chat_info, R.id.chat_content};
    //modified:
    simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.line_of_chat, chatsCursor, fromColumns, toViews, 0);
    ListView listView = (ListView) rootView.findViewById(R.id.chat_text_display);

    listView.setAdapter(simpleCursorAdapter);
    databaseHelper.close();
}

And to link it all together, call dataChanged() from the Listener when the data changes. Note that if this Runnable is running on it's own Thread, you will need to use runOnUiThread():

public class Listener implements Runnable(){
    private MainActivity mainActivity;

    public Listener(MainActivity mainActivity){
        this.mainActivity = mainActivity;
    }

    @Override
    public void run(){
        LogChat logChat = new LogChat();
        logChat.addMessage(message);
        //added:
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 mainActivity.dataChanged();
            }
        });

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!