Home keyboard_arrow_right Questions keyboard_arrow_right #Android
How to connect CalenderView to MySQL database
question
0
Vijayakumar R
April 13, 2018 (edited)
asked 1 year, 10 months ago
viewed 730 times

MainActivity.java

===============

package com.hitech.swad.cal2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Locale;
import android.os.Handler;
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.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  public GregorianCalendar month, itemmonth; // calendar instances.

  public CalendarAdapter adapter; // adapter instance

  public Handler handler; // for grabbing some event values for showing the dot

  // marker.

  public ArrayList < String > items; // container to store calendar items which

  // needs showing the event marker

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Locale.setDefault(Locale.US);

    month = (GregorianCalendar) GregorianCalendar.getInstance();

    itemmonth = (GregorianCalendar) month.clone();

    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));

    RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);

    previous.setOnClickListener(new OnClickListener() {

      @Override

      public void onClick(View v) {

        setPreviousMonth();

        refreshCalendar();

      }

    });

    RelativeLayout next = (RelativeLayout) findViewById(R.id.next);

    next.setOnClickListener(new OnClickListener() {

      @Override

      public void onClick(View v) {

        setNextMonth();

        refreshCalendar();

      }

    });

    gridview.setOnItemClickListener(new OnItemClickListener() {

      public void onItemClick(AdapterView << ? > parent, View v,

        int position, long id) {

        ((CalendarAdapter) parent.getAdapter()).setSelected(v);

        String selectedGridDate = CalendarAdapter.dayString

          .get(position);

        String[] separatedTime = selectedGridDate.split("-");

        String gridvalueString = separatedTime[2].replaceFirst("^0*",

          ""); // taking last part of date. ie; 2 from 2012-12-02.

        int gridvalue = Integer.parseInt(gridvalueString);

        // navigate to next or previous month on clicking offdays.

        if ((gridvalue > 10) && (position < 8)) {

          setPreviousMonth();

          refreshCalendar();

        } else if ((gridvalue < 7) && (position > 28)) {

          setNextMonth();

          refreshCalendar();

        }

        ((CalendarAdapter) parent.getAdapter()).setSelected(v);

        showToast(selectedGridDate);

      }

    });

  }

  protected void setNextMonth() {

    if (month.get(GregorianCalendar.MONTH) == month

      .getActualMaximum(GregorianCalendar.MONTH)) {

      month.set((month.get(GregorianCalendar.YEAR) + 1),

        month.getActualMinimum(GregorianCalendar.MONTH), 1);

    } else {

      month.set(GregorianCalendar.MONTH,

        month.get(GregorianCalendar.MONTH) + 1);

    }

  }

  protected void setPreviousMonth() {

    if (month.get(GregorianCalendar.MONTH) == month

      .getActualMinimum(GregorianCalendar.MONTH)) {

      month.set((month.get(GregorianCalendar.YEAR) - 1),

        month.getActualMaximum(GregorianCalendar.MONTH), 1);

    } else {

      month.set(GregorianCalendar.MONTH,

        month.get(GregorianCalendar.MONTH) - 1);

    }

  }

  protected void showToast(String string) {

    Toast.makeText(this, string, Toast.LENGTH_SHORT).show();

  }

  public void refreshCalendar() {

    TextView title = (TextView) findViewById(R.id.title);

    adapter.refreshDays();

    adapter.notifyDataSetChanged();

    handler.post(calendarUpdater); // generate some calendar items

    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));

  }

  public Runnable calendarUpdater = new Runnable() {

    @Override

    public void run() {

      items.clear();

      // Print dates of the current week

      DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

      String itemvalue;

      for (int i = 0; i < 7; i++) {

        itemvalue = df.format(itemmonth.getTime());

        itemmonth.add(GregorianCalendar.DATE, 1);

        items.add("2018-04-12");

        items.add("2018-05-07");

        items.add("2018-04-15");

        items.add("2018-03-20");

        items.add("2018-04-30");

        items.add("2018-03-28");

      }

      adapter.setItems(items);

      adapter.notifyDataSetChanged();

    }

  };

}

=================

CalendarAdapter.java

=================

package com.hitech.swad.cal2;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {

  private Context mContext;

  private java.util.Calendar month;

  public GregorianCalendar pmonth; // calendar instance for previous month

  /**

  * calendar instance for previous month for getting complete view

  */

  public GregorianCalendar pmonthmaxset;

  private GregorianCalendar selectedDate;

  int firstDay;

  int maxWeeknumber;

  int maxP;

  int calMaxP;

  int lastWeekDay;

  int leftDays;

  int mnthlength;

  String itemvalue, curentDateString;

  DateFormat df;

  private ArrayList < String > items;

  public static List < String > dayString;

  private View previousView;

  public CalendarAdapter(Context c, GregorianCalendar monthCalendar) {

    CalendarAdapter.dayString = new ArrayList < String > ();

    Locale.setDefault(Locale.US);

    month = monthCalendar;

    selectedDate = (GregorianCalendar) monthCalendar.clone();

    mContext = c;

    month.set(GregorianCalendar.DAY_OF_MONTH, 1);

    this.items = new ArrayList < String > ();

    df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

    curentDateString = df.format(selectedDate.getTime());

    refreshDays();

  }

  public void setItems(ArrayList < String > items) {

    for (int i = 0; i != items.size(); i++) {

      if (items.get(i).length() == 1) {

        items.set(i, "0" + items.get(i));

      }

    }

    this.items = items;

  }

  public int getCount() {

    return dayString.size();

  }

  public Object getItem(int position) {

    return dayString.get(position);

  }

  public long getItemId(int position) {

    return 0;

  }

  // create a new view for each item referenced by the Adapter

  public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    TextView dayView;

    if (convertView == null) { // if it's not recycled, initialize some

      // attributes

      LayoutInflater vi = (LayoutInflater) mContext

        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      v = vi.inflate(R.layout.calendar_item, null);

    }

    dayView = (TextView) v.findViewById(R.id.date);

    // separates daystring into parts.

    String[] separatedTime = dayString.get(position).split("-");

    // taking last part of date. ie; 2 from 2012-12-02

    String gridvalue = separatedTime[2].replaceFirst("^0*", "");

    // checking whether the day is in current month or not.

    if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {

      // setting offdays to white color.

      dayView.setTextColor(Color.WHITE);

      dayView.setClickable(false);

      dayView.setFocusable(false);

    } else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {

      dayView.setTextColor(Color.WHITE);

      dayView.setClickable(false);

      dayView.setFocusable(false);

    } else {

      // setting curent month's days in blue color.

      dayView.setTextColor(Color.BLUE);

    }

    if (dayString.get(position).equals(curentDateString)) {

      setSelected(v);

      previousView = v;

    } else {

      v.setBackgroundResource(R.drawable.list_item_background);

    }

    dayView.setText(gridvalue);

    // create date string for comparison

    String date = dayString.get(position);

    if (date.length() == 1) {

      date = "0" + date;

    }

    String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);

    if (monthStr.length() == 1) {

      monthStr = "0" + monthStr;

    }

    // show icon if date is not empty and it exists in the items array

    ImageView iw = (ImageView) v.findViewById(R.id.date_icon);

    if (date.length() > 0 && items != null && items.contains(date)) {

      iw.setVisibility(View.VISIBLE);

    } else {

      iw.setVisibility(View.INVISIBLE);

    }

    return v;

  }

  public View setSelected(View view) {

    if (previousView != null) {

      previousView.setBackgroundResource(R.drawable.list_item_background);

    }

    previousView = view;

    view.setBackgroundResource(R.drawable.calendar_cel_selectl);

    return view;

  }

  public void refreshDays() {

    // clear items

    items.clear();

    dayString.clear();

    Locale.setDefault(Locale.US);

    pmonth = (GregorianCalendar) month.clone();

    // month start day. ie; sun, mon, etc

    firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);

    // finding number of weeks in current month.

    maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);

    // allocating maximum row number for the gridview.

    mnthlength = maxWeeknumber * 7;

    maxP = getMaxP(); // previous month maximum day 31,30....

    calMaxP = maxP - (firstDay - 1); // calendar offday starting 24,25 ...

    /**

    * Calendar instance for getting a complete gridview including the three

    * month's (previous,current,next) dates.

    */

    pmonthmaxset = (GregorianCalendar) pmonth.clone();

    /**

    * setting the start date as previous month's required date.

    */

    pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);

    /**

    * filling calendar gridview.

    */

    for (int n = 0; n < mnthlength; n++) {

      itemvalue = df.format(pmonthmaxset.getTime());

      pmonthmaxset.add(GregorianCalendar.DATE, 1);

      dayString.add(itemvalue);

    }

  }

  private int getMaxP() {

    int maxP;

    if (month.get(GregorianCalendar.MONTH) == month

      .getActualMinimum(GregorianCalendar.MONTH)) {

      pmonth.set((month.get(GregorianCalendar.YEAR) - 1),

        month.getActualMaximum(GregorianCalendar.MONTH), 1);

    } else {

      pmonth.set(GregorianCalendar.MONTH,

        month.get(GregorianCalendar.MONTH) - 1);

    }

    maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);

    return maxP;

  }

}
3 Answers
Android Pal @pal April 13, 2018

Your code looks fine as far as the Android part of your project.

If you want to get MySQL data you'll have to create a simple back-end and serve your data.


There a many options, you could use PHP, Javascript, Python to create a simple RESTful API.

Or you could use a back-end framework like Laravel, SailsJS, Django or others depending on what exactly you want to do and whether you already are using something in the backend.

1
Vijayakumar R @r.vijay155 April 13, 2018 (edited)

I have php mysql connection but data not added to grid. How to add???


package com.hitech.swad.cal2;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import android.os.Handler;
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.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {

  public GregorianCalendar month, itemmonth;// calendar instances.

  public CalendarAdapter adapter;// adapter instance
  public Handler handler;// for grabbing some event values for showing the dot
  // marker.
  public ArrayList<String> items; // container to store calendar items which
  // needs showing the event marker
  String GET_JSON_DATA_HTTP_URL = "http://10.0.2.2/event_php/service_details.php";
  public static final String JSON_TITLE_NAME = "date";

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Locale.setDefault( Locale.US );
    month = (GregorianCalendar) GregorianCalendar.getInstance();
    itemmonth = (GregorianCalendar) month.clone();

    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));

    RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);

    previous.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        setPreviousMonth();
        refreshCalendar();
      }
    });

    RelativeLayout next = (RelativeLayout) findViewById(R.id.next);
    next.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        setNextMonth();
        refreshCalendar();

      }
    });

    gridview.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v,
                  int position, long id) {

        ((CalendarAdapter) parent.getAdapter()).setSelected(v);
        String selectedGridDate = CalendarAdapter.dayString
            .get(position);
        String[] separatedTime = selectedGridDate.split("-");
        String gridvalueString = separatedTime[2].replaceFirst("^0*",
            "");// taking last part of date. ie; 2 from 2012-12-02.
        int gridvalue = Integer.parseInt(gridvalueString);
        // navigate to next or previous month on clicking offdays.
        if ((gridvalue > 10) && (position < 8)) {
          setPreviousMonth();
          refreshCalendar();
        } else if ((gridvalue < 7) && (position > 28)) {
          setNextMonth();
          refreshCalendar();
        }
        ((CalendarAdapter) parent.getAdapter()).setSelected(v);

        showToast(selectedGridDate);

      }
    });

    getData();
  }

  private void getData(){
    //Showing a progress dialog while our app fetches the data from url


    //Creating a json array request to get the json from our api
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
        new Response.Listener<JSONArray>() {
          @Override
          public void onResponse(JSONArray response) {
            //Dismissing the progressdialog on response


            //Displaying our grid
            showGrid(response);
          }
        },
        new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {

          }
        }
    );

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    //Adding our request to the queue
    requestQueue.add(jsonArrayRequest);
  }

  private void showGrid(JSONArray jsonArray){
    
    for(int i = 0; i<jsonArray.length(); i++){
      //Creating a json object of the current index
      JSONObject obj = null;
      try {
        //getting json object from current index
        obj = jsonArray.getJSONObject(i);

        String date = obj.getString(JSON_TITLE_NAME);         
        items.add(date);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }

    adapter.setItems(items);
    adapter.notifyDataSetChanged();
  }

  protected void setNextMonth() {
    if (month.get(GregorianCalendar.MONTH) == month
        .getActualMaximum(GregorianCalendar.MONTH)) {
      month.set((month.get(GregorianCalendar.YEAR) + 1),
          month.getActualMinimum(GregorianCalendar.MONTH), 1);
    } else {
      month.set(GregorianCalendar.MONTH,
          month.get(GregorianCalendar.MONTH) + 1);
    }

  }

  protected void setPreviousMonth() {
    if (month.get(GregorianCalendar.MONTH) == month
        .getActualMinimum(GregorianCalendar.MONTH)) {
      month.set((month.get(GregorianCalendar.YEAR) - 1),
          month.getActualMaximum(GregorianCalendar.MONTH), 1);
    } else {
      month.set(GregorianCalendar.MONTH,
          month.get(GregorianCalendar.MONTH) - 1);
    }

  }

  protected void showToast(String string) {
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show();

  }

  public void refreshCalendar() {
    TextView title = (TextView) findViewById(R.id.title);

    adapter.refreshDays();
    adapter.notifyDataSetChanged();
    handler.post(calendarUpdater); // generate some calendar items

    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
  }

  public Runnable calendarUpdater = new Runnable() {

    @Override
    public void run() {
      items.clear();

      // Print dates of the current week
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
      String itemvalue;
      for (int i = 0; i < 7; i++) {
        itemvalue = df.format(itemmonth.getTime());
        itemmonth.add(GregorianCalendar.DATE, 1);
        items.add("2018-04-13");
        items.add("2018-04-07");
        items.add("2018-04-15");
        items.add("2018-03-20");
        items.add("2018-05-30");
        items.add("2018-04-28");
      }

      adapter.setItems(items);
      adapter.notifyDataSetChanged();
    }
  };
}


===========

When i add default values its works fine.. 

        items.add("2018-04-13");
        items.add("2018-04-07");
        items.add("2018-04-15");
        items.add("2018-03-20");
        items.add("2018-05-30");
        items.add("2018-04-28");


=========

but these below lines not added to "items"..

private void showGrid(JSONArray jsonArray){
    
    for(int i = 0; i<jsonArray.length(); i++){
      //Creating a json object of the current index
      JSONObject obj = null;
      try {
        //getting json object from current index
        obj = jsonArray.getJSONObject(i);

        String date = obj.getString(JSON_TITLE_NAME);         
        items.add(date);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }


How to solve this...


https://www.facebook.com/groups/android.developer.kh/permalink/1770224366370965

0
arrow_upward 1
Your code looks fine, so if it's not working there could be many things which went wrong. Why don't you put it (the whole project) online somewhere (Git) and have a sample JSON data file so it can be tested by other people.
arrow_upward 1
Your code looks fine, so if it's not working there could be many things which went wrong. Why don't you put it (the whole project) online somewhere (Git) and have a sample JSON data file so it can be tested by other people.

sign in Sign in to add your reply or comment.

delete_forever
Careful now...
Doing this you will loose this information and the action cannot be undone.
This action cannot be undone.
cancel