我在我的应用程序中使用countdowntimer,其中我在简单的dateformat中显示日期,但我的countdowntimer没有显示,简单的时钟正在工作,我想显示我的日历,如countdowntimer.Here是我的代码。
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;
}
}
}
谢谢。
答案 0 :(得分:1)
package com.example.diffrence;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.test);
Timer notificationtask = new Timer();
notificationtask.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd/M/yyyy hh:mm:ss");
try {
Date date1 = simpleDateFormat.parse("10/10/2014 11:30:10");
String currentDateandTime = simpleDateFormat.format(new Date());
Date date2 = simpleDateFormat.parse(currentDateandTime);
txt.setText("time Remainig to date 10/10/2014 11:30:10 : " + printDifference(date1,date2));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}, 1000, 1000);
}
public String printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
return Math.abs(elapsedDays) + " days " +
Math.abs(elapsedHours)+ " hours " +
Math.abs(elapsedMinutes)+ " minute " +
Math.abs(elapsedSeconds) + " Seconds ";
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}