Java tutorial
/* * Copyright 2011 Lauri Nevala. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package mad.codeforcommunity; import java.util.ArrayList; import java.util.Calendar; import java.util.Random; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.text.format.Time; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class CalendarView extends Activity { public Calendar month; public CalendarAdapter adapter; public Handler handler; public ArrayList<String> items; // container to store some random calendar items public final Activity curr = this; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.calendar); month = Calendar.getInstance(); // onNewIntent(getIntent()); items = new ArrayList<String>(); adapter = new CalendarAdapter(this, month); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(adapter); handler = new Handler(); handler.post(calendarUpdater); TextView title = (TextView) findViewById(R.id.title); title.setText(android.text.format.DateFormat.format("MMMM yyyy", month)); TextView previous = (TextView) findViewById(R.id.previous); previous.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (month.get(Calendar.MONTH) == month.getActualMinimum(Calendar.MONTH)) { month.set((month.get(Calendar.YEAR) - 1), month.getActualMaximum(Calendar.MONTH), 1); } else { month.set(Calendar.MONTH, month.get(Calendar.MONTH) - 1); } refreshCalendar(); } }); TextView next = (TextView) findViewById(R.id.next); next.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (month.get(Calendar.MONTH) == month.getActualMaximum(Calendar.MONTH)) { month.set((month.get(Calendar.YEAR) + 1), month.getActualMinimum(Calendar.MONTH), 1); } else { month.set(Calendar.MONTH, month.get(Calendar.MONTH) + 1); } refreshCalendar(); } }); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { TextView date = (TextView) v.findViewById(R.id.date); ImageView image = (ImageView) v.findViewById(R.id.date_icon); if (date instanceof TextView && !date.getText().equals("")) { if (image.getVisibility() == View.VISIBLE) { /*OLD CODE * Intent intent = new Intent(); String day = date.getText().toString(); if(day.length()==1) { day = "0"+day; } // return chosen date as string format intent.putExtra("date", android.text.format.DateFormat.format("yyyy-MM", month)+"-"+day); setResult(RESULT_OK, intent); finish(); */ Bundle extras = new Bundle(); String day = date.getText().toString(); if (day.length() == 1) { day = "0" + day; } // return chosen date as string format extras.putString("date", android.text.format.DateFormat.format("yyyy-MM", month) + "-" + day); Intent intent = new Intent(CalendarView.this, EventView.class); intent.putExtras(extras); startActivity(intent); } } } }); } public void refreshCalendar() { TextView title = (TextView) findViewById(R.id.title); adapter.refreshDays(); adapter.notifyDataSetChanged(); handler.post(calendarUpdater); // generate some random calendar items title.setText(android.text.format.DateFormat.format("MMMM yyyy", month)); } //Method to load the event view. Used by the button click and menu option public void loadEventView(View v) { TextView date = (TextView) v.findViewById(R.id.date); ImageView image = (ImageView) v.findViewById(R.id.date_icon); if (date instanceof TextView && !date.getText().equals("")) { if (image.getVisibility() == View.VISIBLE) { /*OLD CODE * Intent intent = new Intent(); String day = date.getText().toString(); if(day.length()==1) { day = "0"+day; } // return chosen date as string format intent.putExtra("date", android.text.format.DateFormat.format("yyyy-MM", month)+"-"+day); setResult(RESULT_OK, intent); finish(); */ Bundle extras = new Bundle(); String day = date.getText().toString(); if (day.length() == 1) { day = "0" + day; } // return chosen date as string format extras.putString("date", android.text.format.DateFormat.format("yyyy-MM", month) + "-" + day); Intent intent = new Intent(CalendarView.this, EventView.class); intent.putExtras(extras); startActivity(intent); } } } public void onNewIntent(Intent intent) { String date = intent.getStringExtra("date"); String[] dateArr = date.split("-"); // date format is yyyy-mm-dd month.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2])); } public Runnable calendarUpdater = new Runnable() { //query database to find days in this month that have events public void run() { String JSONString = null; items.clear(); //Code should be correct but DB calls are not tested ArrayList<NameValuePair> argrsList = new ArrayList<NameValuePair>(); String dateValue = android.text.format.DateFormat.format("yyyy-MM", month).toString(); argrsList.add(new BasicNameValuePair("date", dateValue)); try { JSONString = QueryDb.queryDatabase("events_by_month.php", argrsList);//returns JSON String of days in the month that have events, String needs to be parsed items.addAll(QueryDb.getEventDays(JSONString)); // format random values. You can implement a dedicated class to provide real values // for(int i=0;i<31;i++) { // Random r = new Random(); // // if(r.nextInt(10)>6) // { // items.add(Integer.toString(i)); // } // } adapter.setItems(items); adapter.notifyDataSetChanged(); } catch (HttpConnectionFailedThrowable e) { Toast.makeText(curr, "No Internet Connection", 5000).show(); } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); MenuInflater blowUp = getMenuInflater(); blowUp.inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.monthI: //returns to the current month Time today = new Time(Time.getCurrentTimezone()); Intent intent = new Intent(this, CalendarView.class); intent.putExtra("date", today.year + "-" + today.month + "-" + today.monthDay); startActivityForResult(intent, 1); break; case R.id.dayI: setContentView(findViewById(R.layout.calendar_item)); //returns to the current date loadEventView(findViewById(R.layout.calendar_item)); break; case R.id.createI: //go to webpage to create new event //setContentView(R.layout.create_layout); Intent createInt = new Intent(this, CreateEvent.class); startActivity(createInt); break; case R.id.refreshI: //refresh the calendar refreshCalendar(); break; } return true; } @Override public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub super.onOptionsMenuClosed(menu); } }