2017-11-11 91 views
0

我將數據從選項卡片段插入到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); 
    } 
} 

}

回答

0

你每次當你切換標籤的時間刷新選項卡。爲此,您必須創建一個Interface,如下所示。

public interface MainHomeFragmentInterface { 
    void fragmentBecameVisible(); 
} 

調用此InterfaceviewPager

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

      } 
     }); 

,並在你的每個tabfragment實現這個Interface

0

爲此,你需要添加下 資源的XML文件> XML> 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> 

我希望這可以幫助你! :)

1

您可以使用本地廣播來通知顯示需要刷新的數據的片段。

當您更改數據庫中的數據時,您應該使用LocalBroadcastManager發送廣播,並在顯示數據的片段內部設置BroadcastReceiver

請參閱How to use LocalBroadcastManager?

相關問題