I have a Timer in my onCreate(), it should set a new text when value when the timeString changes. Unfortunately right now it doesn’t change at all.
Here is a snippet of my code. I don’t know what I’ve missed? If you guys have any suggestion then please let me know.
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;
}
}
You have this line:
I’m assuming you have a
TextView txtDurationValue;in your adapter which you haven’t included in your question source code? If it isn’t in there add this inside the adapter. (Technically you shouldn’t expose raw values/views from the adapter, you should use get/sets but this should work until you refactor):You should then be able add
Which will mean you can change this //pager.getAdapter().notifyDataSetChanged(); to:
What it doesn’t do is take care of the fact that you’re setting off a timer in onCreate() which is getting fired before the elements have been instantiated.
This is my favourite Android hack (you can use
indicatoror any other view which you have reference to. Doesn’t really matter in this instance). It fires code directly after the view has been displayed: