I am using countdowntimer in my application,in which I am showing date in simple dateformat but my countdowntimer is not showing , simple clock is working in that, I want to show my calender like countdowntimer.Here is my code.
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;
}
}
}
thank you.