Joe
Joe

Reputation: 397

How to refresh tabhost tab content every time i click the tab?

I have tabhost with 3 tabs.

SONG LIST, NEW SONGS and FAVORITES

In every tab, my content is linearlayout. Inside linearlayout i have listview.

Here is my code..

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false);

        TabHost host = (TabHost) rootView.findViewById(R.id.tabHost);
        host.setup();

        dbHelper = new FragmentOne_DbAdapter(getActivity());
        dbHelper.open();
        //Clean all data
        //dbHelper.deleteAllPlayer1();
        //Add some data
        dbHelper.insertPlayer1Songlist();
        //Generate ListView from SQLite Database

        //Tab 1
        TabHost.TabSpec spec = host.newTabSpec("SONG LIST");
        spec.setContent(R.id.tab1);
        spec.setIndicator("SONG LIST");
        host.addTab(spec);

        listView = (ListView) rootView.findViewById(R.id.slPlayer1ListView);
            player1ESearch = (EditText) rootView.findViewById(R.id.player1Search);
            ImageButton dplayer1ESearch=(ImageButton) rootView.findViewById(R.id.delete);
            dplayer1ESearch.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    player1ESearch.setText("");
                }
            });
            displayPlayer1ListView();

        //Tab 2
        spec = host.newTabSpec("NEW SONGS");
        spec.setContent(R.id.tab2);
        spec.setIndicator("NEW SONGS");
        host.addTab(spec);

            listViewNew = (ListView) rootView.findViewById(R.id.slPlayer1NewListView);
            displayPlayer1NewListView();

        //Tab 3
        spec = host.newTabSpec("FAVORITES");
        spec.setContent(R.id.tab3);
        spec.setIndicator("FAVORITES");
        host.addTab(spec);

        return rootView;
    }

    private void displayPlayer1ListView() {
        Cursor cursor = dbHelper.fetchAllPlayer1();

        FragmentOneAdapter = new FragmentOne_Adapter(getActivity(), cursor, 0);
        listView.setAdapter(FragmentOneAdapter);

        player1ESearch.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                FragmentOneAdapter.getFilter().filter(s.toString());
            }
        });

        FragmentOneAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return dbHelper.fetchPlayer1ByTitle(constraint.toString());
            }
        });
    }

    private void displayPlayer1NewListView() {
        Cursor cursor = dbHelper.fetchAllPlayer1New();
        FragmentOneAdapterNew = new FragmentOne_AdapterNew(getActivity(), cursor, 0);
        listViewNew.setAdapter(FragmentOneAdapterNew);
    }

How can the tab content refresh every time i click the tab?

in my 1st tab SONG LIST,

i update some field of the data,

then when i view NEW SONGS tab,

the update should appear in the list..

what i need is just recall the query or refresh the page every time i click the NEW SONGS tab. so that it will reload and update the content.

thanks!

Upvotes: 4

Views: 2189

Answers (2)

QuokMoon
QuokMoon

Reputation: 4425

Actually issue is your activity already render and in your case you want to refresh your item whenever user tab on the list . You can do this by couple of way :

1)Override OnResume() method in your class than add the code of item which are fetch and update your UI in this lifecycle method.

2)Add tabhost Intent.FLAG_ACTIVITY_CLEAR_TOP while setup a tab .

  i.e
  tabHost.addTab(tabHost.newTabSpec("tab1")
    .setIndicator("First Text")
    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    .setContent(new Intent(this, class1.class)));

Upvotes: 2

TapanHP
TapanHP

Reputation: 6201

If you want to refresh ListView you have to call a simple method its very easy it is notifyDataSetChanged() ,see this answer for more info, so in your case whenever you click on any tab just call that method it will always get refresh your list and make force the listview to set it again as per new list so you can detect your listview as per latest changes occurred at any instance of time.

see the developer documentation here.

Upvotes: 0

Related Questions