1

Developing an application using ActionBarSherlock, I have two tabs in the same. Both tabs have listview as shown in the image. I need to "refresh" the list of the active tab using the refresh button on the bar. What is the best possible approach to achieve the same?

How to call notifyDataSetChanged of NetworkInformationUI from MainActivity?

Below is the code snippet:

public class MainActivity extends SherlockFragmentActivity {
    // Declare Variables
    private FragmentTabHost mTabHost;

    public boolean onOptionsItemSelected(MenuItem item) {

        Toast.makeText(getApplicationContext(), "Refresh Clicked", Toast.LENGTH_LONG).show();

        return true;
    }   

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            menu.add("Refresh")
                .setIcon(R.drawable.ic_action_refresh)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

            return true;
        }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the view from main_fragment.xml
        setContentView(R.layout.main_fragment);


        // Locate android.R.id.tabhost in main_fragment.xml
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        // Create the tabs in main_fragment.xml
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);

        // Create Tab1 with a custom image in res folder
        mTabHost.addTab(mTabHost.newTabSpec("Network Information").setIndicator("Network Information"), 
                NetworkInformationUI.class, null);

        // Create Tab2
        mTabHost.addTab(mTabHost.newTabSpec("Trouble-Shoot N/W").setIndicator("Trouble-Shoot N/W"),
                TroubleShootNWUI.class, null);




        }
}

enter image description here

Rohit
  • 6,941
  • 17
  • 58
  • 102

2 Answers2

1

How to call notifyDataSetChanged of NetworkInformationUI from MainActivity?

When you click the refresh menu item use the FragmentManager to get a reference to the tab fragments, which will be added to the layout in a FragmentTransaction using the tag set on the TabHost.TabSpec("Network Information" for the first fragment, for example, or you could use your own tag through the TabSpec.setTag() method):

int currentTab = mTabHost.getCurrentTab();
if (currentTab == 0) { // first tab is currently selected
    NetworkInformationUI ni = (NetworkInformationUI) getSupportFragmentManager().findFragmentByTag("Network Information");
    // update the list
} else {
    TroubleShootNWUI tnui = (TroubleShootNWUI) getSupportFragmentManager().findFragmentByTag("Trouble-Shoot N/W");
    // update the list
}
user
  • 86,916
  • 18
  • 197
  • 190
  • Thanks for the info, but if, i try to update list using adapter.notifyDataSetChanged(); it doesnot work..any idea? – Rohit Nov 02 '13 at 07:14
  • @PowerPC Without seeing the code for those fragments I can't help you. – user Nov 02 '13 at 07:21
  • i resolved the issue by populating the list again. refer: http://stackoverflow.com/questions/18602457/how-refresh-a-listview-inside-sherlockfragment – Rohit Nov 02 '13 at 07:23
0

This worked for me

FragmentTabHost tabHost3 = (FragmentTabHost) findViewById(android.R.id.tabhost);
                        String current_tab = tabHost3.getCurrentTabTag();
                        if (current_tab.equals("abc")) { 
                            ClassFragment ni = (ClassFragment) getSupportFragmentManager().findFragmentByTag("abc");
                            ni.refresh();
                        }
Aniket-Shinde
  • 213
  • 3
  • 9