Thursday, March 15, 2012

How to display Current Week in android List Activity?


When the program was written the date was 15 Mar 2012............so you will find the output below in screenshots....


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;


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


public class WeekDisplayActivity extends ListActivity {
ArrayList<String> dateStringArray=new ArrayList<String>();
// String[] dateStringArray;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
    }
    
    protected ListAdapter createAdapter()
    {
   
    Calendar c = Calendar.getInstance();


// Set the calendar to monday of the current week
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);


// Print dates of the current week starting on Monday
DateFormat df = new SimpleDateFormat("EEE dd/MM/yyyy");
for (int i = 0; i < 7; i++) {
dateStringArray.add(df.format(c.getTime()));
c.add(Calendar.DATE, 1);
}

    // 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 output..............



No comments:

Post a Comment