M Pascual
M Pascual

Reputation: 55

get events from database and placing on calendar view (android)

I have a calendar program in my android app. Now I need to get events stored in an online Mysql database and place them on the calendar. Now I have a php file that create a json object. And this JSON object shows all the events coming from database

Calendar.php

<?php
include 'DatabaseConfig.php';

// Create connection
//$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
    $conn = mysqli_connect("localhost", "id2553265_admin", "admin", "id2553265_mobile_app");
if ($conn->connect_error) {

 die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM calendar";

$result = $conn->query($sql);

if ($result->num_rows >0) {


 while($row[] = $result->fetch_assoc()) {

 $tem = $row;

 $json = json_encode($tem);


 }

} else {
 echo "No Results Found.";
}
 echo $json;
$conn->close();
?>

In this code I insert 1496134800000L to highlight the date of 2017-05-30. And now I have the java code that highlight and color them blue with a specific date.

@Override

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_calendar, container, false);
        //calendar setup
        compactCalendar = (CompactCalendarView) view.findViewById(R.id.comCalendarView);
        compactCalendar.setUseThreeLetterAbbreviation(true);
        calendarMonth = (TextView) view.findViewById(R.id.calMonth);



        Event may30 = new Event(Color.BLUE, **1496134800000L**, "test");
        compactCalendar.addEvent(may30);

        Event aug17 = new Event(Color.BLUE, 1502960400000L, "test");
        compactCalendar.addEvent(aug17);


        compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
            @Override
            public void onDayClick(Date dateClicked){
                if (dateFormatDay.format(dateClicked).toString().compareTo("**2017-05-30**") == 0){
                    Toast.makeText(CalendarFragment.this.getContext(), "There is an event", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(getActivity(), ListMay07.class);
                    i.putExtra("Date", "Sample date");
                    startActivity(i);

                }else  if (dateFormatDay.format(dateClicked).toString().compareTo("2017-08-17") == 0){
                    Toast.makeText(CalendarFragment.this.getContext(), "There is an event", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(getActivity(), ListAug17.class);
                    i.putExtra("Date", "Sample date");
                    startActivity(i);

                }else{
                    Toast.makeText(CalendarFragment.this.getContext(), "No Event", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onMonthScroll(Date firstDayOfNewMonth) {
                calendarMonth.setText(dateFormatMonth.format(firstDayOfNewMonth));
            }



        });
        // Inflate the layout for this fragment
        return view;

    }

My main problem is how to get the data from the database and insert it in my java code so that all of the events will be enter in the database not in the actual code.

enter image description here

I want to do that by using the JSON object but I don't know how.

[{  
   "ID":"1",
   "Epoch_Time":"1496134800000L",
   "Date":"2017-05-30"   ‌​,
   "Description":"Cama‌​raderie Day"
},
{  
   "ID":"2",
   "Epoch_Time":"1502960400000L",
   "Date":"2017-0‌​8-17",
   "Description":‌​"Sample Date"
}]

Upvotes: 0

Views: 2691

Answers (1)

Saran Sankaran
Saran Sankaran

Reputation: 2606

First create a POJO for the data contained in the JSON.

public class Event{
   long id;
   long epochTime;
   String date;
   String  desc;

  public Event (long id, long epochTime, String date, String  desc){
      this.id = id;
      this.epochTime = epochTime;
      this.date = date;
      this.desc = desc;
  }
}

Then you can parse the JSON and the store it in the POJO

String myJsonResponse // will contain the json response from the server

try {
    JSONArray array = new JSONArray(myJsonResponse);

    List<Event> events = new ArrayList<>(array.length());

    for (int i = 0; i < array.length(); i++) {
        JSONObject object = (JSONObject) array.get(1);
        long id = Long.parseLong(object.getString("ID"));
        long epoctTime = Long.parseLong(object.getString("Epoch_Time"));
        String date = object.getString("Date");
        String description = object.getString("Description");

        Event event = new Event(id, epoctTime, date, description);
        events.add(event);

    }
} catch (JSONException e){
    e.printStackTrace();
}

Then you can use the event list to do whatever you want.

Upvotes: 1

Related Questions