6

I have a situation for my android app.The scenario is I am using a viewpage adapter whering I have 3 pages which are actually are fragments.My issues is that I want to update these from a dialog activity .The dialog activity containg a list from whose onclick event I want to update the fragment.

First of all is it possible if yes please do chip in and help me out.

Cheers Gaurav

This is my view page adapter

/*
 * Copyright (C) 2012 Gaurav Rawat
 * 
 */
package com.punksrant.ifinddryday.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.punksrant.ifinddryday.fragment.AllFragment;
import com.punksrant.ifinddryday.fragment.HomeFragment;
import com.punksrant.ifinddryday.fragment.SynchFragment;
import com.punksrant.ifinddryday.utils.IFindDryDaysConstants;
import com.viewpagerindicator.TitleProvider;

public class ViewPagerAdapter extends FragmentPagerAdapter implements
        TitleProvider {

    private static String[] titles = IFindDryDaysConstants.titles;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public String getTitle(int position) {
        return titles[position % titles.length].toUpperCase();
    }

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

    @Override
    public Fragment getItem(int position) {
        Fragment fragment;
        switch (position) {
        case 0:
            fragment = HomeFragment.newInstance();
            break;
        case 1:
            fragment = AllFragment.newInstance();
            break;

        case 2:
            fragment = SynchFragment.newInstance(titles[position
                    % titles.length]);
            break;

        default:
            fragment = HomeFragment.newInstance();
        }
        return fragment;
    }

}
' and in this I want to refresh the home fragment from my dialog class 
'/*
 * Copyright (C) 2012 Gaurav Rawat
 * 
 */
package com.punksrant.ifinddryday.activity;

import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
import com.google.inject.Inject;
import com.punksrant.ifinddryday.model.Coordinates;
import com.punksrant.ifinddryday.utils.DryDayJSONResParser;
import com.punksrant.ifinddryday.utils.IFindDryDayViewUtils;

@ContentView(R.layout.statedialog)
public class Dialog extends RoboSherlockFragmentActivity {
    @InjectView(R.id.dialogList)
    ListView listView;
    @Inject
    SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] states = DryDayJSONResParser.getSupportedStates();
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, states));
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String state = ((TextView) view).getText().toString();
                Coordinates coordinates = IFindDryDayViewUtils
                        .getCoordinatesFromPreferences(preferences);
                coordinates.setState(state);
                IFindDryDayViewUtils.updateCityFromLocation(coordinates,
                        preferences);

                finish();
            }

        });
    }
},
hsz
  • 148,279
  • 62
  • 259
  • 315
Gaurav Rawat
  • 1,294
  • 1
  • 25
  • 52
  • Where is your viewpage adapter?. Can you post some code of the activity that holds the viewpager?. – gutiory Apr 24 '12 at 07:52
  • Hi @gutiory will paste the code once back home ,actually the jist is I am using Jake’s ViewPagerIndicator library.https://github.com/JakeWharton/Android-ViewPagerIndicator with fragments being added from the fragmentpageadapter .Shall post the code as soon I am back. – Gaurav Rawat Apr 24 '12 at 10:21
  • Can you post the code where ViewPagerAdapter is used?. – gutiory Apr 25 '12 at 05:41
  • 1
    fianlly this [link](http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view) solved my issue – Gaurav Rawat Apr 25 '12 at 06:15
  • 1
    Finally found a solutions at [viewpager-pageadapter-not-updating-the-view][1] [1]: http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view cheers to the guys there – Gaurav Rawat Apr 25 '12 at 06:18
  • 1
    Add as a proper answer please. – Warpzit Apr 12 '13 at 13:33

1 Answers1

1

(credits to blahdiblah rui.araujo) There are several ways to achieve this.

The first option is easier, but bit more inefficient.

Override getItemPosition in your PagerAdapter like this:

public int getItemPosition(Object object) {
    return POSITION_NONE;
}

This way, when you call notifyDataSetChanged(), the view pager will remove all views and reload them all. As so the reload effect is obtained.

The second option, suggested by alvarolb, is to setTag() method in instantiateItem() when instantiating a new view. Then instead of using notifyDataSetChanged(), you can use findViewWithTag() to find the view you want to update.

The second approach is very flexible and high performant. Kudos to alvarolb for the original research.

Community
  • 1
  • 1
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66