Friday, December 28, 2018

java - Android - Output List of events from Google calendar

I would ask You for a help. Now I am writing an app, which takes current events from Google calendars. But problem is there, when I'm trying to output events from more than one calendar. I know, that I'm doing something wrong, but really, can't get in the problem..






public class Main extends Activity {


//TextView mInfo;
TextView mEvents;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mInfo = (TextView)findViewById(R.id.info);
mEvents = (TextView)findViewById(R.id.events);


String[] calendarName = {"Calendar1", "Calendar2", "Calendar3", "Calendar4", "Calendar5"};

readCalendars(calendarName);
}

private void readCalendars(String[] calendarName)
{
String[] projection =
new String[]{

CalendarContract.Calendars._ID,
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.ACCOUNT_NAME,
CalendarContract.Calendars.ACCOUNT_TYPE};
Cursor calCursor =
getContentResolver().
query(CalendarContract.Calendars.CONTENT_URI,
projection,
CalendarContract.Calendars.VISIBLE + " = 1",
null,

CalendarContract.Calendars._ID + " ASC");
long calId = -1;
//String calendarAccount = "";
for(int i = 0; i 0)
{
if(cursor.moveToFirst())
{
do
{
long eventId = cursor.getLong(3);

events += getEventInfo(eventId,cursor.getLong(1),cursor.getLong(2),cursor.getString(4),cursor.getString(5),cursor.getString(6)) + "\n";
} while(cursor.moveToNext());
}
}
mEvents.setText(events);
}
}

@SuppressLint("SimpleDateFormat")
private String getEventInfo(long eventId,long startTime, long endTime,String description, String location,String title)

{
String eventInfo = "";
{
DateFormat df = new SimpleDateFormat("HH:mm");
String start = df.format( new Date(startTime));
String end = df.format( new Date(endTime));
eventInfo = start + "-" + end + " Class: "+ location + "\nLecture: " + title + "\nLecturer: " + description;
}
return eventInfo;
}

}




Tried to output all events with loop, but does not helps. If I pass in calendarName[i], where i is one of existing calendars, I get output of calendar, which I asked for, but that's only one calendar, but for example, I need to output 5 calendars.



Tried to do anything with ListView - failure again.



Hope, You would help with this problem or help somehow how to resolve it..

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...