我在我的應用程序中使用countdowntimer,我在簡單的dateformat中顯示日期,但我的countdowntimer沒有顯示,簡單的時鐘正在工作,我想顯示我的日曆像countdowntimer.Here是我的代碼。Android如何使倒計時像倒計時鐘?
public class MainActivity extends Activity {
ListView mListView;
MyCountDownTimer mMyCountDownTimer;
TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this);
mListView.setAdapter(adapter);
mMyCountDownTimer = new MyCountDownTimer(1000, 1000, adapter);
}
@Override
protected void onResume() {
super.onResume();
mMyCountDownTimer.start();
}
@Override
protected void onPause() {
mMyCountDownTimer.cancel();
super.onPause();
}
//countdowntimerclass
public class MyCountDownTimer extends CountDownTimer {
BaseAdapter mAdapter;
public MyCountDownTimer(long millisInFuture, long countDownInterval, BaseAdapter adapter) {
super(millisInFuture, countDownInterval);
mAdapter = adapter;
}
@Override
public void onFinish() {
mAdapter.notifyDataSetChanged();
this.start();
}
@Override
public void onTick(long millisUntilFinished) {
text1.setText("" + millisUntilFinished/1000);
}
}
public class MyAdapter extends BaseAdapter {
private static final int COUNT = 50;
private boolean firstscroll=true;
private Context context;
private DateFormat dateFormat;
public MyAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return COUNT;
}
@Override
public String getItem(int position) {
return "Item " + String.valueOf(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.two_line_list_item, parent, false);
}
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.US);
String formattedDate = df.format(c.getTime());
text1 = (TextView) convertView.findViewById(android.R.id.text1);
if (position==0) {
text1.setText(getItem(position).valueOf(formattedDate));
}
else {
text1.setText(null);
}
return convertView;
}
private CharSequence getTimeString(int position) {
if (position % 2 == 0) {
// return dateFormat.format(Calendar.getInstance().getTime());
}
else {
return null;
}
return null;
}
private Context getContext() {
return context;
}
}
}
謝謝。
它可以幫助你! [http://stackoverflow.com/questions/8857590/android-countdowntimer-skips-last-ontick][1] [1]:http://stackoverflow.com/questions/ 8857590/android-countdowntimer-skips-last-ontick –