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();
}
});
}
},
OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.
You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.
This function is deprecated.
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With