2012-02-02 50 views
2

1)如何從ViewPagerActivity(其延伸地圖)值發送到PagerAdapter即lat和長時間?如何更新PagerAdapter TextView中(使用後內部instantiateItem充氣)

2)如何更新TextView的PagerAdapter

附:我嘗試getItemPosition並返回POSITION_NONE,但它不工作

ViewPagerActivity

public class ViewPagerActivity extends MapActivity { 
    private ViewPagerAdapter adapter; 
    private ViewPager pager; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     adapter = new ViewPagerAdapter(this); 
     pager = (ViewPager) findViewById(R.id.viewpager); 
     TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator); 
     pager.setAdapter(adapter); 
     indicator.setViewPager(pager); 

     // Brief 
     updateWithNewLocation(location); 
    } 

    private void updateWithNewLocation(Location location) { 
     String latLongString; 

     if (location != null) { 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      latLongString = "Lat:" + lat + "\nLong:" + lng; 
     } else { 
      latLongString = "No location found"; 
     } 
     // Is is possible to set text here 
     // myLocationText = (TextView)findViewById(R.id.myLocationText); 
     // myLocationText.setText("Your Current Position is:\n"+latLongString); 
    } 
} 

ViewPagerAdapter

public class ViewPagerAdapter extends PagerAdapter implements TitleProvider { 
    private final Context context; 
    private MapView mapView; 
    private MapController mapController; 
    private TextView myLocationText; 

    private View view; 

    public ViewPagerAdapter(Context context) { 
    } 

    private static String[] titles = new String[] { "Page1", "Map" }; 

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

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

    @Override 
    public Object instantiateItem(View pager, final int position) { 
     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); 

      mapController = mapView.getController(); 
      mapController.setZoom(14); 
     } 

     return view; 
    } 

    @Override 
    public void destroyItem(View pager, int position, Object view) { 
     ((ViewPager) pager).removeView((View) view); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view.equals(object); 
    } 

    @Override 
    public void finishUpdate(View view) { 
    } 

    @Override 
    public void restoreState(Parcelable p, ClassLoader c) { 
    } 

    @Override 
    public Parcelable saveState() { 
     return null; 
    } 

    @Override 
    public void startUpdate(View view) { 
    } 
} 

回答

0

我得到了解決,但它可能不乾淨的解決方案。如果你們有任何建議,請分享你的想法。 (可能是片段instantiateItem或Notifydatachange,但我不知道如何使用)

將數據發送到ViewPagerAdapter我使用SharedPreferences 更新數據,我使用Singleton模式

ViewPagerActivity--update

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--update

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; 
    } 
} 
相關問題