public View getView(int position, View convertView, ViewGroup parent) { Entity item = elements.get(position); View v = null; if (null == convertView) { v = View.inflate(mContext, R.layout.row_icon, null); } else { v = convertView; } ImageView img = (ImageView) v.findViewById(R.id.img_icon); img.setImageBitmap(ImageUtils.getBitmapAvatar(item.getId(), Utils.AVATAR_LARGE)); return v; }
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putLong(DataFramework.KEY_ID, currentEntity.getId()); }
@Override public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final Entity ent = (Entity) getChild(groupPosition, childPosition); // Set an id depending on the table ent belongs to int entId; if (ent.getTable() == "homework") { entId = 1; } else if (ent.getTable() == "assignment") { entId = 2; } else if (ent.getTable() == "exam") { entId = 3; } else { entId = 4; } // Depending on the id, do something different. For the first three, the only bit that changes // is the string in the alertDialog and the activity // that's opened when you press the element on the list. I'll comment due homework list, the // other are the same. switch (entId) { // Due homework list case 1: convertView = View.inflate(AgendaDay.this, R.layout.agendaday_dueitem, null); try { Button title = (Button) convertView.findViewById(R.id.title); ImageView remove = (ImageView) convertView.findViewById(R.id.dueremove); TextView dueclass = (TextView) convertView.findViewById(R.id.dueclass); LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.linearLayout1); // When editing this element, get the date it was set, not the date you're viewing. final Calendar c = Calendar.getInstance(); c.setTimeInMillis(ent.getLong("setDate")); // Set text title.setText("- " + ent.getString("title")); // If the homework is done, strike it through. if (ent.getInt("done") == 1) { title.setPaintFlags(title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); layout.setBackgroundColor(getResources().getColor(R.color.grey)); } title.setOnClickListener( new OnClickListener() { // When the element's pressed, open AddHwDialog with all the necessary data to // edit it. public void onClick(View addexam) { Intent i = new Intent(AgendaDay.this, AddHwDialog.class); i.putExtra("homework_id", ent.getId()); i.putExtra("todayCalendar", c.getTimeInMillis()); startActivityForResult(i, ACTIVITY_EDIT); } }); remove.setOnClickListener( new OnClickListener() { public void onClick(View removerecord) { AlertDialog.Builder builder = new AlertDialog.Builder(AgendaDay.this); builder .setMessage(R.string.sureHomework) .setTitle(R.string.sureHomeworkTitle) .setCancelable(true) .setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { dialog.cancel(); } }) .setPositiveButton( R.string.accept, new DialogInterface.OnClickListener() { // Remove the element. public void onClick(DialogInterface dialog, int arg1) { ent.delete(); fillList(); } }); AlertDialog AD = builder.create(); AD.show(); } }); // Get the subject this element is associated with... Entity classEnt = new Entity("class", ent.getLong("class_id")); Entity subject = new Entity("subject", classEnt.getLong("subject_id")); // ...to put it as the name that appears. dueclass.setText(subject.getString("title")); } catch (Exception e) { e.printStackTrace(); } break; // Due assignment list case 2: convertView = View.inflate(AgendaDay.this, R.layout.agendaday_dueitem, null); try { Button title = (Button) convertView.findViewById(R.id.title); ImageView remove = (ImageView) convertView.findViewById(R.id.dueremove); TextView dueclass = (TextView) convertView.findViewById(R.id.dueclass); LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.linearLayout1); final Calendar c = Calendar.getInstance(); c.setTimeInMillis(ent.getLong("setDate")); title.setText("- " + ent.getString("title")); if (ent.getInt("done") == 1) { title.setPaintFlags(title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); layout.setBackgroundColor(getResources().getColor(R.color.grey)); } title.setOnClickListener( new OnClickListener() { public void onClick(View addexam) { Intent i = new Intent(AgendaDay.this, AddAssignDialog.class); i.putExtra("assign_id", ent.getId()); i.putExtra("todayCalendar", c.getTimeInMillis()); startActivityForResult(i, ACTIVITY_EDIT); } }); remove.setOnClickListener( new OnClickListener() { public void onClick(View removerecord) { AlertDialog.Builder builder = new AlertDialog.Builder(AgendaDay.this); builder .setMessage(R.string.sureAssignment) .setTitle(R.string.sureAssignmentTitle) .setCancelable(true) .setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { dialog.cancel(); } }) .setPositiveButton( R.string.accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { ent.delete(); fillList(); } }); AlertDialog AD = builder.create(); AD.show(); } }); Entity classEnt = new Entity("class", ent.getLong("class_id")); Entity subject = new Entity("subject", classEnt.getLong("subject_id")); dueclass.setText(subject.getString("title")); } catch (Exception e) { e.printStackTrace(); } break; // Due exam list case 3: convertView = View.inflate(AgendaDay.this, R.layout.agendaday_dueitem, null); try { Button title = (Button) convertView.findViewById(R.id.title); ImageView remove = (ImageView) convertView.findViewById(R.id.dueremove); TextView dueclass = (TextView) convertView.findViewById(R.id.dueclass); final Calendar c = Calendar.getInstance(); c.setTimeInMillis(ent.getLong("setDate")); title.setText("- " + ent.getString("title")); title.setOnClickListener( new OnClickListener() { public void onClick(View addexam) { Intent i = new Intent(AgendaDay.this, AddExamDialog.class); i.putExtra("exam_id", ent.getId()); i.putExtra("todayCalendar", c.getTimeInMillis()); startActivityForResult(i, ACTIVITY_EDIT); } }); remove.setOnClickListener( new OnClickListener() { public void onClick(View removerecord) { AlertDialog.Builder builder = new AlertDialog.Builder(AgendaDay.this); builder .setMessage(R.string.sureExam) .setTitle(R.string.sureExamTitle) .setCancelable(true) .setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { dialog.cancel(); } }) .setPositiveButton( R.string.accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { ent.delete(); fillList(); } }); AlertDialog AD = builder.create(); AD.show(); } }); Entity classEnt = new Entity("class", ent.getLong("class_id")); Entity subject = new Entity("subject", classEnt.getLong("subject_id")); dueclass.setText(subject.getString("title")); } catch (Exception e) { e.printStackTrace(); } break; // Today's classes list case 4: convertView = View.inflate(AgendaDay.this, R.layout.agendaday_item, null); try { Long subjectid = (Long) ent.getValue("subject_id"); Entity subjectent = new Entity("subject", subjectid); // Get when the class starts and when it ends to... String starttimehour = ent.getString("starttimehour"); // Add a 0 if it's lower than 10, Java doesn't do it automatically. if (Integer.parseInt(starttimehour) < 10) { starttimehour = "0" + starttimehour; } String starttimeminute = ent.getString("starttimeminute"); if (Integer.parseInt(starttimeminute) < 10) { starttimeminute = "0" + starttimeminute; } String endtimehour = ent.getString("endtimehour"); if (Integer.parseInt(endtimehour) < 10) { endtimehour = "0" + endtimehour; } String endtimeminute = ent.getString("endtimeminute"); if (Integer.parseInt(endtimeminute) < 10) { endtimeminute = "0" + endtimeminute; } // ...display it nicely in a TextView String classLength = " (" + starttimehour + ":" + starttimeminute + "-" + endtimehour + ":" + endtimeminute + ")"; TextView subject = (TextView) convertView.findViewById(R.id.subjectname); TextView classLengthText = (TextView) convertView.findViewById(R.id.classlength); subject.setText(subjectent.getString("title")); classLengthText.setText(classLength); LinearLayout hwlist = (LinearLayout) convertView.findViewById(R.id.homeworkLinearLayout); LinearLayout assignlist = (LinearLayout) convertView.findViewById(R.id.assignmentLinearLayout); LinearLayout examlist = (LinearLayout) convertView.findViewById(R.id.examLinearLayout); try { // Start my three arraylists. entHw = new ArrayList<Entity>(); entExams = new ArrayList<Entity>(); entAssignments = new ArrayList<Entity>(); // Get today's date. Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT")); c.set(mYear, mMonth, mDay, 0, 0, 0); c.set(Calendar.MILLISECOND, 0); // Get all homework associated to the class you're viewing. List<Entity> entclassHw = db.getEntityList("homework", "class_id = '" + ent.getId() + "'"); Iterator<Entity> iter = entclassHw.iterator(); // Put in entHw only those that are set on the day you're viewing while (iter.hasNext()) { Entity HwEnt = (Entity) iter.next(); Calendar d = Calendar.getInstance(TimeZone.getTimeZone("GMT")); d.setTimeInMillis(HwEnt.getLong("setDate")); if (d.getTimeInMillis() == c.getTimeInMillis()) { entHw.add(HwEnt); } } List<Entity> entclassAssignments = db.getEntityList("assignment", "class_id = '" + ent.getId() + "'"); Iterator<Entity> iter2 = entclassAssignments.iterator(); while (iter2.hasNext()) { Entity AssignEnt = (Entity) iter2.next(); Calendar d = Calendar.getInstance(TimeZone.getTimeZone("GMT")); d.setTimeInMillis(AssignEnt.getLong("setDate")); if (d.get(Calendar.YEAR) == c.get(Calendar.YEAR) && d.get(Calendar.DAY_OF_YEAR) == c.get(Calendar.DAY_OF_YEAR)) { entAssignments.add(AssignEnt); } } List<Entity> entclassExams = db.getEntityList("exam", "class_id = '" + ent.getId() + "'"); Iterator<Entity> iter3 = entclassExams.iterator(); while (iter3.hasNext()) { Entity ExamEnt = (Entity) iter3.next(); Calendar d = Calendar.getInstance(TimeZone.getTimeZone("GMT")); d.setTimeInMillis(ExamEnt.getLong("setDate")); if (d.get(Calendar.YEAR) == c.get(Calendar.YEAR) && d.get(Calendar.DAY_OF_YEAR) == c.get(Calendar.DAY_OF_YEAR)) { entExams.add(ExamEnt); } } } catch (Exception e) { e.printStackTrace(); } // Get a date for the day you're viewing final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT")); c.set(mYear, mMonth, mDay, 0, 0, 0); c.set(Calendar.MILLISECOND, 0); // For every homework you have on the day your viewing and the class element the list is // at. for (final Entity ent2 : entHw) { LinearLayout linear = new LinearLayout(getApplicationContext()); linear.setOrientation(LinearLayout.HORIZONTAL); LayoutParams linearLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); linear.setLayoutParams(linearLayoutParams); linear.setBackgroundResource(R.color.purple); Button hwTitle = new Button(getApplicationContext()); hwTitle.setText("- " + ent2.getString("title")); if (ent2.getInt("done") == 1) { hwTitle.setPaintFlags(hwTitle.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); } hwTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19); hwTitle.setGravity(Gravity.LEFT); hwTitle.setPadding(5, 5, 0, 5); LayoutParams titleButtonLayoutParams = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1); hwTitle.setLayoutParams(titleButtonLayoutParams); hwTitle.setBackgroundColor(android.R.color.transparent); hwTitle.setOnClickListener( new OnClickListener() { public void onClick(View addexam) { Intent i = new Intent(AgendaDay.this, AddHwDialog.class); i.putExtra("homework_id", ent2.getId()); i.putExtra("todayCalendar", c.getTimeInMillis()); startActivityForResult(i, ACTIVITY_EDIT); } }); ImageView remove = new ImageView(getApplicationContext()); LayoutParams removeButtonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); removeButtonLayoutParams.gravity = Gravity.CENTER_VERTICAL; remove.setBackgroundDrawable(getResources().getDrawable(R.drawable.remove)); remove.setLayoutParams(removeButtonLayoutParams); remove.setOnClickListener( new OnClickListener() { public void onClick(View removerecord) { AlertDialog.Builder builder = new AlertDialog.Builder(AgendaDay.this); builder .setMessage(R.string.sureHomework) .setTitle(R.string.sureHomeworkTitle) .setCancelable(true) .setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { dialog.cancel(); } }) .setPositiveButton( R.string.accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { ent2.delete(); fillList(); } }); AlertDialog AD = builder.create(); AD.show(); } }); linear.addView(hwTitle); linear.addView(remove); hwlist.addView(linear); } for (final Entity ent2 : entAssignments) { LinearLayout linear = new LinearLayout(getApplicationContext()); linear.setOrientation(LinearLayout.HORIZONTAL); LayoutParams linearLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); linear.setLayoutParams(linearLayoutParams); linear.setBackgroundResource(R.color.light_green); Button assignTitle = new Button(getApplicationContext()); assignTitle.setText("- " + ent2.getString("title")); if (ent2.getInt("done") == 1) { assignTitle.setPaintFlags( assignTitle.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); } assignTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); assignTitle.setGravity(Gravity.LEFT); assignTitle.setPadding(5, 5, 0, 5); LayoutParams titleButtonLayoutParams = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1); assignTitle.setLayoutParams(titleButtonLayoutParams); assignTitle.setBackgroundColor(android.R.color.transparent); assignTitle.setOnClickListener( new OnClickListener() { public void onClick(View addexam) { Intent i = new Intent(AgendaDay.this, AddAssignDialog.class); i.putExtra("assign_id", ent2.getId()); i.putExtra("todayCalendar", c.getTimeInMillis()); startActivityForResult(i, ACTIVITY_EDIT); } }); ImageView remove = new ImageView(getApplicationContext()); LayoutParams removeButtonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); removeButtonLayoutParams.gravity = Gravity.CENTER_VERTICAL; remove.setBackgroundDrawable(getResources().getDrawable(R.drawable.remove)); remove.setLayoutParams(removeButtonLayoutParams); remove.setOnClickListener( new OnClickListener() { public void onClick(View removerecord) { AlertDialog.Builder builder = new AlertDialog.Builder(AgendaDay.this); builder .setMessage(R.string.sureAssignment) .setTitle(R.string.sureAssignmentTitle) .setCancelable(true) .setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { dialog.cancel(); } }) .setPositiveButton( R.string.accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { ent2.delete(); fillList(); } }); AlertDialog AD = builder.create(); AD.show(); } }); linear.addView(assignTitle); linear.addView(remove); assignlist.addView(linear); } for (final Entity ent2 : entExams) { LinearLayout linear = new LinearLayout(getApplicationContext()); linear.setOrientation(LinearLayout.HORIZONTAL); LayoutParams linearLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); linear.setLayoutParams(linearLayoutParams); linear.setBackgroundResource(R.color.light_orange); Button examTitle = new Button(getApplicationContext()); examTitle.setText("- " + ent2.getString("title")); examTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); examTitle.setGravity(Gravity.LEFT); examTitle.setPadding(5, 5, 0, 5); LayoutParams titleButtonLayoutParams = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1); examTitle.setLayoutParams(titleButtonLayoutParams); examTitle.setBackgroundColor(android.R.color.transparent); examTitle.setOnClickListener( new OnClickListener() { public void onClick(View addexam) { Intent i = new Intent(AgendaDay.this, AddExamDialog.class); i.putExtra("exam_id", ent2.getId()); i.putExtra("todayCalendar", c.getTimeInMillis()); startActivityForResult(i, ACTIVITY_EDIT); } }); ImageView remove = new ImageView(getApplicationContext()); LayoutParams removeButtonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); removeButtonLayoutParams.gravity = Gravity.CENTER_VERTICAL; remove.setBackgroundDrawable(getResources().getDrawable(R.drawable.remove)); remove.setLayoutParams(removeButtonLayoutParams); remove.setOnClickListener( new OnClickListener() { public void onClick(View removerecord) { AlertDialog.Builder builder = new AlertDialog.Builder(AgendaDay.this); builder .setMessage(R.string.sureExam) .setTitle(R.string.sureExamTitle) .setCancelable(true) .setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { dialog.cancel(); } }) .setPositiveButton( R.string.accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { ent2.delete(); fillList(); } }); AlertDialog AD = builder.create(); AD.show(); } }); linear.addView(examTitle); linear.addView(remove); examlist.addView(linear); } ImageView add = (ImageView) convertView.findViewById(R.id.add); add.setOnClickListener( new OnClickListener() { public void onClick(View addsubject) { Intent i = new Intent(AgendaDay.this, AddAssignmentListDialog.class); i.putExtra("class_id", ent.getId()); Calendar c = Calendar.getInstance(); c.set(mYear, mMonth, mDay, 0, 0, 0); c.set(Calendar.MILLISECOND, 0); c.setTimeZone(TimeZone.getTimeZone("GMT")); i.putExtra("todayCalendar", c.getTimeInMillis()); startActivityForResult(i, ACTIVITY_CREATE); } }); } catch (Exception e) { e.printStackTrace(); } break; } return convertView; }