Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Refresh Fragment from Action Bar Menu
    primarykey
    data
    text
    <p>I have an Action Bar with Fragment as follow. I would like to refresh current fragment using Refresh button Action Bar Menu. I saw a lot of example using getFragmentByTag() but my fragment is created dynamically. May I know how to get the current fragment and refresh the content.</p> <p><pre> <code> public class MainActivity extends FragmentActivity implements ActionBar.TabListener {</p> RssFragmentPagerAdapter mRssFragmentPagerAdapter; ViewPager mViewPager; List&lt;RssCategory&gt; categoryList; // Database Helper private DatabaseHelper db; private ActionBar actionBar; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try{ db = DatabaseHelper.getInstance(getApplicationContext()); int categoryCount = db.getCategoriesCount(); // Create the adapter that will return a fragment for each of the three primary sections // of the app. mRssFragmentPagerAdapter = new RssFragmentPagerAdapter(getSupportFragmentManager(), categoryCount); // Set up the action bar. actionBar = getActionBar(); // Specify that the Home/Up button should not be enabled, since there is no hierarchical // parent. actionBar.setHomeButtonEnabled(false); // Specify that we will be displaying tabs in the action bar. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); // Set up the ViewPager, attaching the adapter and setting up a listener for when the // user swipes between sections. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mRssFragmentPagerAdapter); mViewPager.setOffscreenPageLimit(categoryCount - 1); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // When swiping between different app sections, select the corresponding tab. // We can also use ActionBar.Tab#select() to do this if we have a reference to the // Tab. actionBar.setSelectedNavigationItem(position); } }); initialiseActionBar(); }catch(Exception e){ Log.e(getClass().getName(), e.getMessage()); } } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: this.finish(); return true; case R.id.action_refresh: //TO REFRESH CURRENT Fragment return true; default: return super.onOptionsItemSelected(item); } } private void initialiseActionBar() { if(categoryList == null) categoryList = db.getAllCategories(); // For each of the sections in the app, add a tab to the action bar. for (RssCategory category : categoryList) { // Create a tab with text corresponding to the page title defined by the adapter. // Also specify this Activity object, which implements the TabListener interface, as the // listener for when this tab is selected. actionBar.addTab( actionBar.newTab() .setText(category.getName()) .setTabListener(this)); } } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary * sections of the app. */ public static class RssFragmentPagerAdapter extends FragmentPagerAdapter { private int pageCount; public RssFragmentPagerAdapter(FragmentManager fm, int pageCount) { super(fm); this.pageCount = pageCount; } @Override public Fragment getItem(int i) { switch (i) { default: // The other sections of the app are dummy placeholders. Fragment fragment = new RssFragment(); Bundle args = new Bundle(); args.putInt(RssFragment.ARG_CATEGORY_ID, i + 1); fragment.setArguments(args); return fragment; } } @Override public int getCount() { return pageCount; } /*@Override public CharSequence getPageTitle(int position) { return "Section " + (position + 1); }*/ } /** * A dummy fragment representing a section of the app, but that simply displays dummy text. */ public static class RssFragment extends Fragment { public static final String ARG_CATEGORY_ID = "category_id"; View rootView; private List&lt;RssItem&gt; resultList; List&lt;RssWebSite&gt; websiteList; ArrayList&lt;String&gt; urlList; ProgressBar progressBar; @SuppressWarnings("unchecked") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { try{ rootView = inflater.inflate(R.layout.fragment_rss_items_list, container, false); resultList = new ArrayList&lt;RssItem&gt;(); progressBar = (ProgressBar)rootView.findViewById(R.id.progressBar); Bundle args = getArguments(); if(args != null){ DatabaseHelper db = DatabaseHelper.getInstance(rootView.getContext()); websiteList = db.getAllRssWebSiteByCategory(args.getInt(ARG_CATEGORY_ID)); urlList = new ArrayList&lt;String&gt;(); if(websiteList != null &amp;&amp; websiteList.size() &gt; 0){ for (RssWebSite website : websiteList) { urlList.add(website.getRssUrl()); } if(urlList.size() &gt; 0) { GetRSSDataTask task = new GetRSSDataTask(); task.execute(urlList); } } } }catch(Exception e){ Log.e(getClass().getName(), e.getMessage()); } return rootView; } /** * This class downloads and parses RSS Channel feed. * * @author clippertech * */ private class GetRSSDataTask extends AsyncTask&lt;ArrayList&lt;String&gt;, Void, List&lt;RssItem&gt; &gt; { @Override protected List&lt;RssItem&gt; doInBackground(ArrayList&lt;String&gt;... urls) { try { for(String url : urls[0]) { // Create RSS reader RssReader rssReader = new RssReader(url); Log.d(getClass().getName(), url); // Parse RSS, get items resultList.addAll(rssReader.getItems()); } return resultList; }catch (Exception e) { Log.e(getClass().getName(), e.getMessage()); } return null; } @Override protected void onPostExecute(List&lt;RssItem&gt; result) { try{ // Get a ListView from the RSS Channel view ListView itcItems = (ListView) rootView.findViewById(R.id.rssChannelListView); View emptyView = null; if(result == null){ itcItems.setEmptyView(emptyView); Log.d(getClass().getName(), "Empty View"); } else { //resultList.addAll(result); Collections.sort(result, new Comparator&lt;RssItem&gt;() { @Override public int compare(RssItem lhs, RssItem rhs) { SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); try { Date date1 = formatter.parse(rhs.getPublishedDate()); Date date2 = formatter.parse(lhs.getPublishedDate()); return date1.compareTo(date2); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }); // Create a list adapter ListAdapter adapter = new ListAdapter(rootView.getContext(), resultList); itcItems.setAdapter(adapter); adapter.notifyDataSetChanged(); // Set list view item click listener itcItems.setOnItemClickListener(new ListListener(resultList, getActivity())); } //dialog.dismiss(); progressBar.setVisibility(View.GONE); }catch(Exception e){ Log.e(getClass().getName(), e.getMessage()); } } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload