无法从计时器更新PageAdapter的TextView

时间:2012-02-09 04:48:14

标签: android android-viewpager

我的onCreate()中有一个Timer,当timeString发生变化时,它应该在值时设置一个新文本。不幸的是,它现在根本没有变化。

以下是我的代码片段。我不知道我错过了什么?如果你们有任何建议,请告诉我。

ViewPagerActivity

public class ViewPagerActivity extends MapActivity {
    private ViewPagerAdapter adapter;
    private ViewPager pager;
    private String timeString;
    /** 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);

        Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
                public void run() {
                    timerMethod();
                }
            }, 0, 1000);
    }

    private void timerMethod() {
        this.runOnUiThread(doTimer);
    }

    private Runnable doTimer = new Runnable() {
            private int totalSecs;
            private int hours;
            private int minutes;
            private int seconds;

            public void run() {

                totalSecs += 1;
                hours = totalSecs / 3600;
                minutes = (totalSecs % 3600) / 60;
                seconds = totalSecs % 60;

                timeString = (String) (hours > 9 ? String.valueOf(hours): AddZero(hours)) + ":" +
                    (String) (minutes > 9 ? String.valueOf(minutes): AddZero(minutes)) + ":"
                    +  (String) (seconds > 9 ? String.valueOf(seconds): AddZero(seconds));

                //pager.getAdapter().notifyDataSetChanged();

            }

            private String AddZero(int text) {
                return "0"+ text;
            }
        };
}

ViewPagerAdapter

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

    public ViewPagerAdapter(Context context) {
            this.context = context;
    }

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

    @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);
        //here, set text at first time when view was create only
        //and not update,when value of timeString change
        txtDurationValue = (TextView)view.findViewById(R.id.txtDurationValue);
        txtDurationValue.setText(timeString);

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

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

}

1 个答案:

答案 0 :(得分:1)

你有这一行:

txtDurationValue = (TextView)view.findViewById(R.id.txtDurationValue);

我假设您的适配器中有一个TextView txtDurationValue;,但您的问题源代码中没有这个问题?如果不在那里,请在适配器内添加。 (从技术上讲,你不应该从适配器公开原始值/视图,你应该使用get / sets,但这应该可以工作,直到你重构):

public TextView txtDurationValue;

然后您应该可以添加

public void setText(String text) {
    txtDurationValue.setText(text);
}

这意味着您可以更改此//pager.getAdapter()。notifyDataSetChanged();到:

((ViewPagerAdapter)pager.getAdapter()).setText(text);

它没有做的是处理你在onCreate()中设置一个定时器的事实,它在元素被实例化之前被触发。

这是我最喜欢的Android黑客(你可以使用indicator或你引用的任何其他视图。在这个例子中并不重要)。它在显示视图后直接激活代码:

    ViewTreeObserver vto = indicator .getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    myTimer.schedule(new TimerTask() {
                        public void run() {
                           timerMethod();
                        }
                    }, 0, 1000);
    });