4

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!

Joe
  • 387
  • 1
  • 7
  • 22

2 Answers2

2

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)));
QuokMoon
  • 4,387
  • 4
  • 26
  • 50
0

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.

Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66
  • have you saw this answer, i have already tag a question in my answer. http://stackoverflow.com/a/21862750/5476209 ? this is another way – TapanHP Jul 13 '16 at 04:13
  • 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 everytime i click the NEW SONGS tab. so that it will reload na update i mage in SONG LIST tab. – Joe Jul 13 '16 at 04:20
  • so you have to put it in sqlite database and every time when you call the new tab page bt clicking a tab then just get all the tracks from sqlite database every time, but if you want it to be more fast and efficient then first put the condition to check whether any changes happened in sqlite? if so then get list from there and pass that list in your adapter so that you will always get the latest list. – TapanHP Jul 13 '16 at 04:29
  • can you help me to construct my code? i update my fragment code.. thanks.. – Joe Jul 13 '16 at 04:32
  • sorry got a job to do, but you got an idea what to do. Do it by your own please. Thanks. – TapanHP Jul 13 '16 at 04:34