I need to create the below android application using android studio. It is a mob
ID: 3883169 • Letter: I
Question
I need to create the below android application using android studio. It is a mobile timetabler.
Project Settings
Your Android application should have the following settings:
Minimum SDK: API 19 Android 4.4
Application Name: Mobile Timetabler
Project Name: MobileTimetabler<your-student-ID-number>
For example, MobileTimetabler30078589
Package name: au.edu.vu.timetable<your-student-ID-number>
For example, au.edu.vu.timetable30078589
If the user then clicks the [Delete appointment(s)] button the DeleteAppointmentActivity is executed. There are two ways that you can write the DeleteAppointmentActivity - a standard way.
DeleteAppointmentActivity - Standard Version
When a DeleteAppointmentActivity is launched, the database is queried for all saved appointments, and each appointment is displayed in a list of dynamically created checkboxes, one appointment per checkbox.
The id value from the database is retrieved (along with all other fields for each appointment), and the program sets the id value of the checkbox to be the id of the appointment. This way, the id value of each checked checkbox is the id value of the appointment we want to delete from the database.
A user can check some, all or none of the checkboxes and then click the [Delete selected] button, which will trigger deletion of the appointments from the database. This is achieved by creating a List of the checkbox id values where the checkbox is checked, and then passing that list to a deleteScheduleItems method which goes through each value in the list and deletes the record with that id from the database.
If the user clicks the [Back] button the activity finishes and we are returned to the MainActivity without making any changes to the database.
An example layout for the standard version of the DeleteAppointmentActivity is shown below in Figure 4:
After deleting the "fly a kite" appointment above, our appointment list in the MainActivity will now look like Figure 5, below:
The MySQLiteHelper class creates our SQLite database.
The DATABASE_CREATE string must contain commands to create the table with the following schema:
COLUMN_ID - integer primary key autoincrement
COLUMN_DAY - integer not null
COLUMN_TIME - text not null
COLUMN_DURATION - text not null
COLUMN_DESCRIPTION - text not null
The database name should be appointments.db
The NewAppointmentActivity allows the user to select a day from a group of RadioButtons, and then enter text (as a String) for the time, duration and description.
If the [Create appointment] button is clicked the appointment is saved to the database. However, if the user opts to create a new appointment but has not entered any time, duration or description information (i.e. all three strings are empty) - then placeholder text is used for each field, which is specified as "<No time>", "<No duration>" and "<No description>" respectively. After saving the appointment the activity finishes and we are returned to the MainActivity.
If the [Back] button is pressed then the activity finishes without saving the appointment and we are again returned to the MainActivity.
Explanation / Answer
ApplicationTest.java
----------------------------------------
import android.app.Application;
import android.test.ApplicationTestCase;
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
-----------------------------------------------------------------
Appointment.java
--------------------------
public class Appointment {
private static final String DELIMITER = ", ";
private static final String DEFAULT_TIME = "No time";
private static final String DEFAULT_DURATION = "No duration";
private static final String DEFAULT_DESCRIPTION = "No description";
private int mId;
private DayClass mDay;
private String mTime;
private String mDuration;
private String mDescription;
public int getId() {
return mId;
}
public void setId(int id) {
this.mId = id;
}
public DayClass getDay() {
return mDay;
}
public void setDay(DayClass day) {
this.mDay = day;
}
public String getTime() {
return mTime;
}
public void setTime(String time) {
if (time == null || time.isEmpty())
this.mTime = DEFAULT_TIME;
else
this.mTime = time;
}
public String getDuration() {
return mDuration;
}
public void setDuration(String duration) {
if (duration == null || duration.isEmpty())
this.mDuration = DEFAULT_DURATION;
else
this.mDuration = duration;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
if (description == null || description.isEmpty())
this.mDescription = DEFAULT_DESCRIPTION;
else
this.mDescription = description;
}
public boolean isValid() {
if (mDay != null && mTime != null && mDuration != null) {
if (!mTime.isEmpty() && !mDuration.isEmpty())
return true;
}
return false;
}
public String toString() {
return mTime + DELIMITER + mDuration + DELIMITER + mDescription;
}
}
------------------------------------------------------------------------------------
AppointmentAdapter.java
-----------------------------------
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class AppointmentAdapter extends CustomAdapter<Appointment> implements OnCheckedChangeListener {
private Map<Integer, Boolean> mCheckedList;
public AppointmentAdapter(Context context, int resource, int textViewResourceId, List<Appointment> objects) {
super(context, resource, textViewResourceId, objects);
this.mCheckedList = new HashMap<>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
CheckBox checkBox;
Appointment appointment = getItem(position); // appointment is not null
if (convertView == null) {
view = mInflater.inflate(mResource, parent, false);
//Log.d(this.getClass().getName(), "null convertView pos : " + position + " , app_id : " + appointment.getId());
checkBox = (CheckBox) view.findViewById(mFieldId);
view.setTag(checkBox);
//Log.d(this.getClass().getName(), "checkBox pos : " + position + " , app_id : " + appointment.getId());
}
else {
view = convertView;
checkBox = (CheckBox) view.getTag();
//Log.d(this.getClass().getName(), "convertView pos : " + position + " , app_id : " + appointment.getId());
}
checkBox.setText(appointment.toString());
checkBox.setTag(appointment);
//checkBox.setChecked(((ListView) parent).isItemChecked(position));
Boolean isChecked = mCheckedList.get(appointment.getId());
if (isChecked != null && isChecked) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(this);
return view;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
Integer id = ((Appointment) buttonView.getTag()).getId();
if (isChecked)
mCheckedList.put(id, true);
else
mCheckedList.remove(id);
} catch (NullPointerException e) {
Log.e(this.getClass().getName(), "Null Pointer" );
}
buttonView.setChecked(isChecked);
}
Set<Integer> getCheckedList() {
return mCheckedList.keySet();
}
}
------------------------------------------------------------------------------------------
AppointmentDataSource.java
-----------------------------------------
import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.List;
public class AppointmentDataSource extends Application {
private static AppointmentDataSource mAppInstance;
private volatile static MySQLiteHelper mHelperInstance;
private SQLiteDatabase mDatabase;
private static final String DELIMITER_1 = ", ";
private static final String DELIMITER_2 = "', '";
@Override
public void onCreate() {
//Log.d(this.getClass().getName(), "Create Start");
super.onCreate();
initResources();
//Log.d(this.getClass().getName(), "Create End");
}
@Override
public void onTerminate() {
super.onTerminate();
//Log.d(this.getClass().getName(), "Terminate");
try {
mDatabase.close();
mHelperInstance.close();
} catch (Exception e) {
Log.d(this.getClass().getName(), "Terminate Exception");
}
}
private void initResources() {
mAppInstance = this;
// DCL (Double Checking Locking) >= JDK 1.4
if (mHelperInstance == null) {
synchronized (MySQLiteHelper.class) {
if (mHelperInstance == null) mHelperInstance = new MySQLiteHelper(getApplicationContext());
}
}
mDatabase = mHelperInstance.getWritableDatabase();
}
public static AppointmentDataSource getInstance() {
return mAppInstance;
}
public void open() {
if (!mDatabase.isOpen()) mHelperInstance.onOpen(mDatabase);
}
private void insert1(Appointment appointment) {
String sqlStr = "INSERT INTO " +
MySQLiteHelper.TABLE_APPOINTMENTS + " (" +
MySQLiteHelper.COLUMN_DAY_NAME + DELIMITER_1 +
MySQLiteHelper.COLUMN_TIME_NAME + DELIMITER_1 +
MySQLiteHelper.COLUMN_DURATION_NAME + DELIMITER_1 +
MySQLiteHelper.COLUMN_DESCRIPTION_NAME + ") VALUES ('" +
DayClass.toInteger(appointment.getDay()) + DELIMITER_2 +
appointment.getTime() + DELIMITER_2 +
appointment.getDuration() + DELIMITER_2 +
appointment.getDescription() + "')";
Log.d(this.getClass().getName(), sqlStr);
Log.d(this.getClass().getName(), "insertAppointment End");
mDatabase.execSQL(sqlStr);
}
private void insert2(Appointment appointment) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_DAY_NAME, DayClass.toInteger(appointment.getDay()));
values.put(MySQLiteHelper.COLUMN_TIME_NAME, appointment.getTime());
values.put(MySQLiteHelper.COLUMN_DURATION_NAME, appointment.getDuration());
values.put(MySQLiteHelper.COLUMN_DESCRIPTION_NAME, appointment.getDescription());
mDatabase.insert(MySQLiteHelper.TABLE_APPOINTMENTS, null, values);
}
public void insertAppointment(Appointment appointment) {
//Log.d(this.getClass().getName(), "insertAppointment Start");
insert2(appointment);
}
private void delete1(String list) {
String sqlStr = "DELETE FROM " +
MySQLiteHelper.TABLE_APPOINTMENTS + " WHERE " +
MySQLiteHelper.COLUMN_ID_NAME + " IN (" +
list + ")";
Log.d(this.getClass().getName(), sqlStr);
mDatabase.execSQL(sqlStr);
}
private void delete2(String list) {
String sqlStr = MySQLiteHelper.COLUMN_ID_NAME + " IN (" + list + ")";
//Log.d(this.getClass().getName(), sqlStr);
mDatabase.delete(MySQLiteHelper.TABLE_APPOINTMENTS, sqlStr, null);
}
public void deleteAppointment(String list) {
if (!list.isEmpty()) {
delete2(list);
}
//Log.d(this.getClass().getName(), "deleteAppointment End");
}
private Appointment cursorToAppointment(Cursor cursor, Appointment appointment) {
if (appointment == null) appointment = new Appointment();
appointment.setId(cursor.getInt(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_ID_NAME)));
appointment.setDay(DayClass.fromInteger(Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_DAY_NAME)))));
appointment.setTime(cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_TIME_NAME)));
appointment.setDuration(cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_DURATION_NAME)));
appointment.setDescription(cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_DESCRIPTION_NAME)));
return appointment;
}
public void getAllGroupAppointments(List<AppointmentGroup> groupList) {
//Log.d(this.getClass().getName(), "getAllGroupAppointments Start");
for (AppointmentGroup group : groupList) {
group.clear();
}
// Read-Only SQLiteDatabase doesn't need to be closed. See Android API
SQLiteDatabase db = mHelperInstance.getReadableDatabase();
Cursor cursor = db.query(MySQLiteHelper.TABLE_APPOINTMENTS, null, null, null, null, null, MySQLiteHelper.ORDER_CONDITION);
Appointment appointment = null;
while (cursor.moveToNext()) {
appointment = cursorToAppointment(cursor, appointment);
DayClass day = appointment.getDay();
if (day != null) {
AppointmentGroup group = groupList.get(DayClass.toInteger(day));
if (group != null && group.add(appointment)) appointment = null;
}
}
cursor.close();
//Log.d(this.getClass().getName(), "getAllGroupAppointments End");
}
public void getAllAppointments(List<Appointment> appointmentList) {
//Log.d(this.getClass().getName(), "getAllAppointments Start");
// Read-Only SQLiteDatabase doesn't need to be closed. See Android API
SQLiteDatabase db = mHelperInstance.getReadableDatabase();
Cursor cursor = db.query(MySQLiteHelper.TABLE_APPOINTMENTS, null, null, null, null, null, MySQLiteHelper.ORDER_CONDITION);
while (cursor.moveToNext()) {
Appointment appointment = cursorToAppointment(cursor, null);
if (appointment.getDay() != null && appointmentList != null) appointmentList.add(appointment);
}
cursor.close();
//Log.d(this.getClass().getName(), appointmentList.toString());
//Log.d(this.getClass().getName(), "getAllAppointments End");
}
private final class MySQLiteHelper extends SQLiteOpenHelper {
private static final String TABLE_APPOINTMENTS = "appointments";
private static final String COLUMN_ID_NAME = "id";
private static final String COLUMN_DAY_NAME = "app_day";
private static final String COLUMN_TIME_NAME = "app_time";
private static final String COLUMN_DURATION_NAME = "duration";
private static final String COLUMN_DESCRIPTION_NAME = "description";
private static final String COLUMN_ID = "integer primary key";
private static final String COLUMN_DAY = "integer not null";
private static final String COLUMN_TIME = "text not null";
private static final String COLUMN_DURATION = "text not null";
private static final String COLUMN_DESCRIPTION = "text not null";
private static final String DATABASE_NAME = "timetable.db";
private static final String DATABASE_CREATE = "CREATE TABLE ";
private static final String ORDER_CONDITION = "app_day, app_time, id";
private static final int DATABASE_VERSION = 1;
private MySQLiteHelper(final Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqlStr = DATABASE_CREATE +
TABLE_APPOINTMENTS + " (" +
COLUMN_ID_NAME + " " + COLUMN_ID + ", " +
COLUMN_DAY_NAME + " " + COLUMN_DAY + ", " +
COLUMN_TIME_NAME + " " + COLUMN_TIME + ", " +
COLUMN_DURATION_NAME + " " + COLUMN_DURATION + ", " +
COLUMN_DESCRIPTION_NAME + " " + COLUMN_DESCRIPTION + ")";
db.execSQL(sqlStr);
//Log.d(this.getClass().getName(), sqlStr);
//Log.d(this.getClass().getName(), "Create End");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sqlStr = "DROP TABLE IF EXISTS " + TABLE_APPOINTMENTS;
db.execSQL(sqlStr);
onCreate(db);
//Log.d(this.getClass().getName(), sqlStr);
//Log.d(this.getClass().getName(), "Upgrade End");
}
}
}
----------------------------------------------------------------------------------------
AppointmentGroup.java
---------------------------------------
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class AppointmentGroup {
private DayClass mDay;
private List<Appointment> mList;
AppointmentGroup(final DayClass day) {
this.mDay = day;
mList = new ArrayList<>();
}
public String getDayName() {
switch (mDay) {
case MONDAY:
return "Monday";
case TUESDAY:
return "Tuesday";
case WEDNESDAY:
return "Wednesday";
case THURSDAY:
return "Thursday";
case FRIDAY:
return "Friday";
default:
return "Unknown Day";
}
}
public boolean add(Appointment appointment) {
//if (!mList.isEmpty()) {
for (Appointment element : mList) {
if (appointment.getId() == element.getId())
return false;
}
//}
mList.add(appointment);
return true;
}
public void clear() {
mList.clear();
}
public String getSchedules() {
if (mList.isEmpty()) {
return "No appointments";
}
StringBuilder sb = new StringBuilder("");
Iterator<Appointment> iterator = mList.iterator();
boolean initialLoop = true;
while (iterator.hasNext()) {
Appointment appointment = iterator.next();
if (initialLoop) initialLoop = false;
else sb.append(" ");
sb.append(appointment.toString());
}
return sb.toString();
}
}
----------------------------------------------------------------------------
AppointmentGroupAdapter.java
-------------------------------------------
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class AppointmentGroupAdapter extends CustomAdapter<AppointmentGroup> {
public AppointmentGroupAdapter(Context context, int resource, int textViewResourceId, List<AppointmentGroup> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
TextView day;
TextView detail;
AppointmentGroup group = getItem(position); // group is not null
if (convertView == null) {
view = mInflater.inflate(mResource, parent, false);
day = (TextView) view.findViewById(R.id.textViewGroupDay);
detail = (TextView) view.findViewById(R.id.textViewGroupSchedule);
view.setTag(new GroupViewHolder(day, detail));
} else {
view = convertView;
GroupViewHolder holder = (GroupViewHolder) view.getTag();
day = holder.day;
detail = holder.detail;
}
if (day != null) day.setText(group.getDayName());
if (detail != null) detail.setText(group.getSchedules());
return view;
}
private final class GroupViewHolder {
TextView day;
TextView detail;
GroupViewHolder(TextView day, TextView detail) {
this.day = day;
this.detail = detail;
}
}
}
--------------------------------------------------------------------------------------
CustomAdapter.java
----------------------------------------------
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomAdapter<T> extends BaseAdapter implements Filterable {
protected List<T> mObjects;
private final Object mLock = new Object();
protected int mResource;
private int mDropDownResource;
protected int mFieldId = 0;
private boolean mNotifyOnChange = true;
private Context mContext;
private ArrayList<T> mOriginalValues;
private ArrayFilter mFilter;
protected LayoutInflater mInflater;
public CustomAdapter(Context context, int resource) {
init(context, resource, 0, new ArrayList<T>());
}
public CustomAdapter(Context context, int resource, int textViewResourceId) {
init(context, resource, textViewResourceId, new ArrayList<T>());
}
public CustomAdapter(Context context, int resource, T[] objects) {
init(context, resource, 0, Arrays.asList(objects));
}
public CustomAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
init(context, resource, textViewResourceId, Arrays.asList(objects));
}
public CustomAdapter(Context context, int resource, List<T> objects) {
init(context, resource, 0, objects);
}
public CustomAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
init(context, resource, textViewResourceId, objects);
}
public void add(T object) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(object);
} else {
mObjects.add(object);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
public void addAll(Collection<? extends T> collection) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.addAll(collection);
} else {
mObjects.addAll(collection);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
public void addAll(T... items) {
synchronized (mLock) {
if (mOriginalValues != null) {
Collections.addAll(mOriginalValues, items);
} else {
Collections.addAll(mObjects, items);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
public void insert(T object, int index) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(index, object);
} else {
mObjects.add(index, object);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
public void remove(T object) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.remove(object);
} else {
mObjects.remove(object);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
public void clear() {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.clear();
} else {
mObjects.clear();
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
public void sort(Comparator<? super T> comparator) {
synchronized (mLock) {
if (mOriginalValues != null) {
Collections.sort(mOriginalValues, comparator);
} else {
Collections.sort(mObjects, comparator);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mNotifyOnChange = true;
}
public void setNotifyOnChange(boolean notifyOnChange) {
mNotifyOnChange = notifyOnChange;
}
private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
mContext = context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mResource = mDropDownResource = resource;
mObjects = objects;
mFieldId = textViewResourceId;
}
public Context getContext() {
return mContext;
}
public int getCount() {
return mObjects.size();
}
public T getItem(int position) {
return mObjects.get(position);
}
public int getPosition(T item) {
return mObjects.indexOf(item);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mResource);
}
private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) {
View view;
TextView text;
if (convertView == null) {
view = mInflater.inflate(resource, parent, false);
} else {
view = convertView;
}
try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("CustomAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException("CustomAdapter requires the resource ID to be a TextView", e);
}
T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence) item);
} else {
text.setText(item.toString());
}
return view;
}
public void setDropDownViewResource(int resource) {
this.mDropDownResource = resource;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mDropDownResource);
}
public static ArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
return new ArrayAdapter<>(context, textViewResId, strings);
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<T> list;
synchronized (mLock) {
list = new ArrayList<>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase();
ArrayList<T> values;
synchronized (mLock) {
values = new ArrayList<>(mOriginalValues);
}
final int count = values.size();
final ArrayList<T> newValues = new ArrayList<>();
for (int i = 0; i < count; i++) {
final T value = values.get(i);
final String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
for (String word : words) {
if (word.startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//noinspection unchecked
mObjects = (List<T>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
----------------------------------------------------------------------------------------------
DayClass.java
---------------------------------------------------
import android.support.annotation.Nullable;
public enum DayClass {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY;//, SATURDAY, SUNDAY;
@Nullable
public static DayClass fromInteger(int day) {
switch (day) {
case 0:
return MONDAY;
case 1:
return TUESDAY;
case 2:
return WEDNESDAY;
case 3:
return THURSDAY;
case 4:
return FRIDAY;
//case 5:
// return SATURDAY;
//case 6:
// return SUNDAY;
}
return null;
}
public static int toInteger(DayClass day) {
switch (day) {
case MONDAY:
return 0;
case TUESDAY:
return 1;
case WEDNESDAY:
return 2;
case THURSDAY:
return 3;
case FRIDAY:
return 4;
//case SATURDAY:
// return 5;
//case SUNDAY:
// return 6;
}
return 7;
}
}
----------------------------------------------------------------------------------------
DeleteAppointmentActivity.java
-------------------------------------------------
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class DeleteAppointmentActivity extends AppCompatActivity {
private List<Appointment> mAppointmentList;
private AppointmentAdapter mListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Log.d(this.getClass().getName(), "Create Start");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_appointment);
initResources();
//Log.d(this.getClass().getName(), "Create End");
}
@Override
protected void onResume() {
//Log.d(this.getClass().getName(), "Resume Start");
super.onResume();
getAllAppointments();
mListAdapter.notifyDataSetChanged();
//Log.d(this.getClass().getName(), "Resume End");
}
private void initResources() {
mAppointmentList = new ArrayList<>();
mListAdapter = new AppointmentAdapter(this, R.layout.appointment_row, R.id.checkboxAppointment, mAppointmentList);
final ListView listView = (ListView) findViewById(R.id.listViewDelete);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(mListAdapter);
}
public void onClickBack(View view) {
finish();
}
public void onClickDelete(View view) {
//Log.d(this.getClass().getName(), "Del Start");
Set<Integer> checkedList = mListAdapter.getCheckedList();
if (!checkedList.isEmpty()) {
StringBuilder sb = new StringBuilder(checkedList.toString());
String strCheckedIdList = sb.substring(1, sb.length() - 1);
AppointmentDataSource.getInstance().deleteAppointment(strCheckedIdList);
Toast.makeText(DeleteAppointmentActivity.this, "Appointment(s) deleted", Toast.LENGTH_SHORT).show();
//Log.d(this.getClass().getName(), "Del End");
finish();
} else {
Toast.makeText(DeleteAppointmentActivity.this, "Appointment(s) should be selected", Toast.LENGTH_SHORT).show();
//Log.d(this.getClass().getName(), "Del End");
}
}
public void getAllAppointments() {
//Log.d(this.getClass().getName(), "getAllAppointments Start");
AppointmentDataSource.getInstance().getAllAppointments(mAppointmentList);
//Log.d(this.getClass().getName(), "getAllAppointments End");
}
}
------------------------------------------------------------------------------------------------------
MainActivity.java
------------------------------------------------
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<AppointmentGroup> mGroupList;
private AppointmentGroupAdapter mListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Log.d(this.getClass().getName(), "Create Start");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initResources();
//Log.d(this.getClass().getName(), "Create End");
}
@Override
public void onResume() {
//Log.d(this.getClass().getName(), "Resume Start");
super.onResume();
getAllAppointments();
mListAdapter.notifyDataSetChanged();
//Log.d(this.getClass().getName(), "Resume End");
}
private void initResources() {
mGroupList = new ArrayList<>();
for (DayClass day : DayClass.values()) {
mGroupList.add(new AppointmentGroup(day));
}
mListAdapter = new AppointmentGroupAdapter(this, R.layout.appointment_group_row, 0, mGroupList);
final ListView listView = (ListView) findViewById(R.id.listViewMain);
listView.setAdapter(mListAdapter);
}
private void getAllAppointments() {
//Log.d(this.getClass().getName(), "getAllAppointments Start");
AppointmentDataSource.getInstance().getAllGroupAppointments(mGroupList);
//Log.d(this.getClass().getName(), "getAllAppointments End");
}
public void createNewAppointment(View view) {
Intent intent = new Intent(this, NewAppointmentActivity.class);
startActivity(intent);
}
public void deleteAppointment(View view) {
Intent intent = new Intent(this, DeleteAppointmentActivity.class);
startActivity(intent);
}
}
---------------------------------------------------------------------------------------
NewAppointmentActivity.java
---------------------------------------------------
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class NewAppointmentActivity extends AppCompatActivity {
private Appointment mAppointment;
private RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Log.d(this.getClass().getName(), "Create Start");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_appointment);
initResources();
//Log.d(this.getClass().getName(), "Create End");
}
@Override
public void onResume() {
//Log.d(this.getClass().getName(), "Resume Start");
super.onResume();
//Log.d(this.getClass().getName(), "Resume End");
}
private void initResources() {
mAppointment = new Appointment();
mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
}
public void onClickBack(View view) {
finish();
}
@Nullable
private DayClass getSelectedDay() {
switch (mRadioGroup.getCheckedRadioButtonId()) {
case R.id.radioButton1:
return DayClass.MONDAY;
case R.id.radioButton2:
return DayClass.TUESDAY;
case R.id.radioButton3:
return DayClass.WEDNESDAY;
case R.id.radioButton4:
return DayClass.THURSDAY;
case R.id.radioButton5:
return DayClass.FRIDAY;
default:
//Log.d(this.getClass().getName(), "Unknown RadioButton Clicked");
return null;
}
}
public void onClickCreate(View view) {
//Log.d(this.getClass().getName(), "New Start");
final EditText editTextTime = (EditText) findViewById(R.id.editTextTime);
final EditText editTextDuration = (EditText) findViewById(R.id.editTextDuration);
final EditText editTextDescription = (EditText) findViewById(R.id.editTextDescription);
mAppointment.setId(0); // Dummy Value : 0
mAppointment.setDay(getSelectedDay());
mAppointment.setTime(editTextTime.getText().toString().trim());
mAppointment.setDuration(editTextDuration.getText().toString().trim());
mAppointment.setDescription(editTextDescription.getText().toString().trim());
if (mAppointment.isValid()) {
AppointmentDataSource.getInstance().insertAppointment(mAppointment);
Toast.makeText(NewAppointmentActivity.this, "Appointment created", Toast.LENGTH_SHORT).show();
//Log.d(this.getClass().getName(), "New End");
finish();
} else {
Toast.makeText(NewAppointmentActivity.this, "Day should be selected", Toast.LENGTH_SHORT).show();
//Log.d(this.getClass().getName(), "New End");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.