Thursday, March 15, 2012

How to display required days ahead of current date in Android ListActivity?


Below is the Example.....................


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;


import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;


public class WeekDisplayActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
    }
    
    protected ListAdapter createAdapter()
    {
    SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 
         GregorianCalendar date = new GregorianCalendar();
         String[] dateStringArray = new String[7];


         for (int day = 0; day < 7; day++) {
             dateStringArray[day] = curFormater.format(date.getTime());
             date.roll(Calendar.DAY_OF_YEAR, true);
         }
   
    // Create a simple array adapter (of type string) with the test values
    ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dateStringArray);

    return adapter;
    }
}




Below is the Screenshot of Output..........



No comments:

Post a Comment