Я получил решение, но оно не может быть чистым.Если у вас есть предложения, пожалуйста, поделитесь своей идеей.(Может быть фрагмент в instantiateItem
или Notifydatachange, но я не знаю, как это использовать)
Для отправки данных в ViewPagerAdapter я использую SharedPreferences Для обновленияданные, я использую шаблон Singleton
ViewPagerActivity - обновление
public class ViewPagerActivity extends MapActivity {
private ViewPagerAdapter updateView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// getInstance from ViewPagerAdapter
updateView = ViewPagerAdapter.getInstance();
}
private void updateWithNewLocation(Location location) {
SharedPreferences items = getSharedPreferences("my_location", 0);
SharedPreferences.Editor editor = items.edit();
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
editor.putString("mylocation", latLongString);
} else {
latLongString = "No location found";
editor.putString("mylocation", latLongString);
}
editor.commit();
if (updateView.getView() != null)
updateView.getMyLocationText().setText(
"Your Current Position is:\n" + latLongString);
}
}
ViewPagerAdapter - Обновление
public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {
private static ViewPagerAdapter core;
private SharedPreferences items;
private String mylocation;
public ViewPagerAdapter(Context context) {
this.context = context;
this.core = this;
}
public static ViewPagerAdapter getInstance() {
return core;
}
@Override
public Object instantiateItem(View pager, final int position) {
items = context.getSharedPreferences("my_location", 0);
mylocation = items.getString("mylocation", "No Response");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (position == 0) {
view = inflater.inflate(R.layout.whereami, null);
((ViewPager) pager).addView(view, 0);
myLocationText = (TextView) view.findViewById(R.id.myLocationText);
myLocationText.setText("Your Current Position is:\n" + mylocation);
}
if (position == 1) {
view = inflater.inflate(R.layout.my_map_activity, null);
((ViewPager) pager).addView(view, 0);
mapView = (MapView) view.findViewById(R.id.mapview);
}
return view;
}
}