B. Money
B. Money

Reputation: 931

How Do I Dynamically Update ViewPagerIndicator Headers?

I'm a noob to android development and I am having trouble dynamically updating Jake Wharton's Viewpagerindicator titles/headers. I have already tried using notifyDataSetChanged() on the indicator instance within the onPageChangeListener method and within another method with no success. I initially set the titles/headers via global string in OnCreate. I have tried to implement the answer from this SO question with no success. Any help solving this is greatly appreciated.

MY CODE

OnCreate

    chosenAilment = "";
    suggestedRemedy="";
    whichAilment=chosenAilment;
    whichRemedy=suggestedRemedy;
    whichArea="AREA OF DISCOMFORT";

    setContentView(R.layout.viewpagermain);
    MyPagerAdapter adapter = new MyPagerAdapter( this );
    pager = (ViewPager)findViewById( R.id.viewpager );
    indicator = (TitlePageIndicator)findViewById( R.id.indicator );
    pager.setAdapter( adapter );
    indicator.setViewPager( pager );
    indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            Crouton.makeText(RemedyActivity.this, "page changed", Style.INFO).show();
            if(position==0){
                whichAilment="";
                whichRemedy="";
                whichArea="AREA OF DISCOMFORT";
            }else if(position==1){
                whichAilment=chosenAilment;
                whichRemedy="";
                whichArea="";
            }else if(position==2){
                whichAilment="";
                whichRemedy=suggestedRemedy;
                whichArea="";
            }
            indicator.notifyDataSetChanged();
        } 

Update Method

 public void updateMsg(String t_info, float t_x, float t_y, int t_c){
     //infoView.updateInfo(t_info, t_x, t_y, t_c);
     popUpButton.setText(TouchView.touchInfo);     
     popUpButton.setX(t_x-(popUpButton.getWidth()/2));
     popUpButton.setY(t_y-(popUpButton.getHeight()));

     Resources res = getResources();
        if(TouchView.touchInfo=="Arm"){
            ailmentsList = res.getStringArray(R.array.arm_ailments);
            chosenAilment = "ARM AILMENTS";
        }else if(TouchView.touchInfo=="Leg"){
            ailmentsList = res.getStringArray(R.array.leg_ailments);
            chosenAilment = "LEG AILMENTS";
        }else if(TouchView.touchInfo=="Back"){
            ailmentsList = res.getStringArray(R.array.back_ailments);
            chosenAilment = "BACK AILMENTS";
        }else if(TouchView.touchInfo=="Groin"){
            if(isMale){
                ailmentsList = res.getStringArray(R.array.groin_ailments_male);
                chosenAilment = "GROIN AILMENTS";
            }else{
                ailmentsList = res.getStringArray(R.array.groin_ailments_female);
                chosenAilment = "GROIN AILMENTS";
            }
        }else if(TouchView.touchInfo=="Chest"){
            if(isMale){
                ailmentsList = res.getStringArray(R.array.chest_ailments_male);
                chosenAilment = "CHEST AILMENTS";
            }else{
                ailmentsList = res.getStringArray(R.array.chest_ailments_female);
                chosenAilment = "CHEST AILMENTS";
            }
        }else if(TouchView.touchInfo=="Abdomen"){
            if(isMale){
                ailmentsList = res.getStringArray(R.array.abdomen_ailments_male);
                chosenAilment = "ABDOMEN AILMENTS";
            }else{
                ailmentsList = res.getStringArray(R.array.abdomen_ailments_female);
                chosenAilment = "ABDOMEN AILMENTS";
            }
        }else if(TouchView.touchInfo=="Head"){
            ailmentsList = res.getStringArray(R.array.head_ailments);
            chosenAilment = "HEAD AILMENTS";
        }else if(TouchView.touchInfo=="Buttocks"){
            ailmentsList = res.getStringArray(R.array.buttocks_ailments);
            chosenAilment = "BUTTOCKS AILMENTS";
        }else{
            ailmentsList = res.getStringArray(R.array.head_ailments);
            chosenAilment = "";
        }

     indicator.notifyDataSetChanged();
  }

Pager Adapter

 private class MyPagerAdapter extends PagerAdapter {

    private String[] titles = new String[] {whichArea,  whichAilment, whichRemedy };
    private final Context context;
    private int[] scrollPosition = new int[titles.length];

    public MyPagerAdapter( Context context )
    {
        this.context = context;
        for ( int i = 0; i < titles.length; i++ )
        {
            scrollPosition[i] = 0;
        }
    }

    @Override
    public String getPageTitle( int position )
    {
        return titles[position];
    }

    @Override
    public int getCount()
    {               
        return titles.length;
    }

Upvotes: 3

Views: 1388

Answers (1)

Rarw
Rarw

Reputation: 7663

The main issues you have are with your pager adapter and update method. Rather than rewrite the whole thing, I suggest you use an existing library like this one or this one by Jake Wharton. It will make your life much easier and does exactly what you are trying to do.

Here is a link to the turotial for how to set up the Jake Wharton ViewPagerIndicator. It provides sample code and near step by step instructions on how to set it up. Just modify the tutorial to meet your needs.

You will notice in the tutorial they use an Array to hold the titles that will be displayed for each view. The titles in the example are pre-defined. I notice you have a titles array in your Adapter. If that is all you need to set, use those. If you don't know what fragments will be added to your ViewPager in advance, you could just key them as you add them to the adapter - for example, use a HashMap as the collection that is backing your adapter that way you can title each Fragment as it added like in this example and then just add those titles (they key values from your hash map) to the ViewPagerIndicator title list as they come up. (Just get the fragment that's displayed and find its key in the HasMap).

Upvotes: 1

Related Questions