SFDEV
SFDEV

Reputation: 3

How to refresh a Listview in a Fragment after data has been added?

I am inserting Data into a sqlite database from a tab fragment. My problem is once data is inserted or deleted from the list, the updated list only appears after the app is restarted. How can i make the fragment refresh after new data is added to the list?

Any help with this would be appreciated.

public Tab1() {
    // Required empty public constructor
}



// TODO: Rename and change types and number of parameters
public static Tab1 newInstance(String param1, String param2) {
    Tab1 fragment = new Tab1();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);

       return fragment;
}



@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);

    }

}


//load tasks into the list
private void loadTaskList() {

    ArrayList<String> taskList = dbHelper.getTaskList();
    if (mAdapter == null) {
        mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.row, R.id.task_title, taskList);
        lstTask.setAdapter(mAdapter);

    } else {
        mAdapter.clear();
        mAdapter.addAll(taskList);
        mAdapter.notifyDataSetChanged();
    }



}


    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View frag1 = inflater.inflate(R.layout.fragment_tab1, container, false);

    dbHelper = new DbHelper(getActivity());
    lstTask = (ListView)frag1.findViewById(R.id.lstTask);

    loadTaskList();

        return frag1;

}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

}

Upvotes: 0

Views: 287

Answers (3)

algrid
algrid

Reputation: 5964

You can use local broadcasts to notify the fragment displaying the data that it needs to refresh.

When you change data in DB you should send a broadcast using LocalBroadcastManager and inside the fragment displaying the data you should set up a BroadcastReceiver.

See How to use LocalBroadcastManager?

Upvotes: 1

sahil kapoor
sahil kapoor

Reputation: 1

for this you need to add an xml file under res>xml>pref_data_sync.xml which will contain

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
        android:defaultValue="180"
        android:entries="@array/pref_sync_frequency_titles"
        android:entryValues="@array/pref_sync_frequency_values"
        android:key="sync_frequency"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null"
        android:title="@string/pref_title_sync_frequency" />

    <Preference android:title="@string/pref_title_system_sync_settings">
        <intent android:action="android.settings.SYNC_SETTINGS" />
    </Preference>

</PreferenceScreen>

in which @array files are included in string.xml as -

<string-array name="pref_sync_frequency_titles">
        <item>15 minutes</item>
        <item>30 minutes</item>
        <item>1 hour</item>
        <item>3 hours</item>
        <item>6 hours</item>
        <item>Never</item>
    </string-array>

    <string-array name="pref_sync_frequency_values">
        <item>15</item>
        <item>30</item>
        <item>60</item>
        <item>180</item>
        <item>360</item>
        <item>-1</item>
    </string-array>

    <string name="pref_title_sync_frequency">Sync frequency</string>

    <string name="pref_title_system_sync_settings">System sync settings</string>

I hope this helps you! :)

Upvotes: 0

Mehul Kabaria
Mehul Kabaria

Reputation: 6632

you have to refresh tab every time when you switch tabs. for that, you have to create one Interface like below.

public interface MainHomeFragmentInterface {
    void fragmentBecameVisible();
}

call this Interface at viewPager

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MainHomeFragmentInterface fragment = (MainHomeFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, position);
                if (fragment != null) {
                    fragment.fragmentBecameVisible();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

and implement this Interface at your each tab fragment.

Upvotes: 0

Related Questions