Update BadgeView from within a TabHost Fragment

608 Views Asked by At

I have a Tabhost in an activity that hosts fragments in each tab. The fragments have a list View that is populated with some elements.

Every Tab has a badge which reflects the no of items in the list in the fragment. I am using Badge from the library Badge Library . Now, the items in the list may change upon an action (for eg delete or add ).

I want the badge for each tab to be updated with every insertion or deletion in their lists. I tried adding a new Badge each time there is an updation in list using following code:-

TabWidget tabs = (TabWidget)  getActivity().findViewById(android.R.id.tabs);

BadgeView allBadge = new BadgeView(getActivity(), tabs, 2);
BadgeView learntBadge = new BadgeView(getActivity(), tabs, 1);
BadgeView newBadge = new BadgeView(getActivity(), tabs, 0);

allBadge.setText(String.valueOf(allWords.getCount()));
learntBadge.setText(String.valueOf(learntWords.getCount()));
newBadge.setText(String.valueOf(newWords.getCount()));
allBadge.toggle();
learntBadge.toggle();
newBadge.toggle();

But this did not work. Any guesses what am I doing wrong here ?

1

There are 1 best solutions below

2
On BEST ANSWER

The Badge Views that are being displayed on TabHost tabs can be made class members of Activity to which Tab Host belongs. The Activity class can have a update member function that takes as input the text to be shown by Badge Views and updates the text being displayed by these BadgeViews with the function input. At the Fragment side, the fragments can get a handle to the parent activity through getActivity and call the update member function of activity while passing the new values to be displayed on Badge View.

Adding BadgeViews to activity

void addBadges()
{
    TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);
    Badge1 = new BadgeView(this, tabs, 2);
    Badge2 = new BadgeView(this, tabs, 1);
    Badge3 = new BadgeView(this, tabs, 0); 
}

The function to update BadgeViews in Activity

public void UpdateBadgeView( String newString1, String newString2, String newString3 )
{
    Badge1.setText(newString1);
    Badge2.setText(newString2);
    Badge3.setText(newString3);
}

The call to Update BadgeViews from Fragment

((MyActivityClass) getActivity()).UpdateBadgeView( text1, text2, text3 );