如何在PagerAdapter中更新textview(在instantiateItem中使用inflater之后)

时间:2012-02-02 03:26:01

标签: android adapter android-viewpager

1.。)如何从 ViewPagerActivity (扩展地图)向 PagerAdapter 发送值,即lat和long?

2.。)如何更新 PagerAdapter中的 Textview

P.S。我尝试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) {
    }
}

1 个答案:

答案 0 :(得分:1)

我得到了解决方案,但它可能无法清除解决方案。如果你们有任何建议,请分享你的想法。 (可能是instantiateItem或Notifydatachange中的碎片,但我不知道如何使用它)

要将数据发送到 ViewPagerAdapter 我使用 SharedPreferences 要更新数据,我使用单一模式

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;
    }
}