如何在添加数据后刷新片段中的Listview?

时间:2017-11-11 05:55:37

标签: android sqlite listview android-fragments

我从一个标签片段将数据插入到sqlite数据库中。我的问题是,一旦从列表中插入或删除数据,更新的列表仅在重新启动应用程序后出现。如何在将新数据添加到列表后刷新片段?

对此有任何帮助将不胜感激。

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

}

3 个答案:

答案 0 :(得分:1)

您可以使用本地广播来通知显示需要刷新的数据的片段。

当您更改数据库中的数据时,您应使用LocalBroadcastManager发送广播,并在显示数据的片段内设置BroadcastReceiver

请参阅How to use LocalBroadcastManager?

答案 1 :(得分:0)

每次切换标签时都必须刷​​新标签。为此,您必须创建一个spinner() { local pid=$! local delay=0.75 local spinstr='|/-\' while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do local temp=${spinstr#?} printf " [%c] " "$spinstr" local spinstr=$temp${spinstr%"$temp"} sleep $delay printf "\b\b\b\b\b\b" done } sleep 5 & spinner | echo -e "\nCopyright of KatworX© Tech. Developed by Arjun Singh Kathait and Debugged by the ☆Stack Overflow Community☆" ,如下所示。

Interface

public interface MainHomeFragmentInterface { void fragmentBecameVisible(); }

处拨打此Interface
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) { } }); Interface上实施此tab

答案 2 :(得分:0)

为此你需要添加一个xml文件 RES&GT; XML&GT; pref_data_sync.xml 其中包含

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

其中@array文件包含在string.xml中作为 -

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

我希望这会对你有所帮助! :)