Android Open Source - digitalcampus Student Adapter






From Project

Back to project page digitalcampus.

License

The source code is released under:

MIT License

If you think the Android project digitalcampus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.llenguatges.digitalcampus.adapters;
/*  w  w w .j a  v  a 2 s .co  m*/
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.llenguatges.digitalcampus.MyApplication;
import com.llenguatges.digitalcampus.R;
import com.llenguatges.digitalcampus.database.StudentSubjectTable;
import com.llenguatges.digitalcampus.database.StudentTable;
import com.llenguatges.digitalcampus.objects.Student;

public class StudentAdapter extends ArrayAdapter<Student> {

  private List<Student> data;
  private Activity activity;
  private MyApplication myApp;
  
  /**
   * Interface definition for a callback to be invoked when a view is clicked.
   */
  private OnClickListener onClick = new OnClickListener() {
    
    private Student student;
    private int pos;
    
    /**
     * Called when a view has been clicked.
     */
    public void onClick(View v) {
      pos = (Integer) v.getTag();
      student = data.get(pos);
      switch(v.getId()){
      case R.id.studentDiscard:
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Delete");
        alertDialogBuilder.setMessage("Delete "+student.getFirstName()+" "+student.getLastName()+"?").setCancelable(false).setPositiveButton("Delete",new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,int id) {
            StudentTable st = new StudentTable(getContext());
            st.DeleteById(student.getId());
            data.remove(pos);
            deleteStudent(student.getId());
            notifyDataSetChanged();
            myApp.setStudents(data);
            dialog.cancel();
          }
        }).setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,int id) {
            dialog.cancel();
          }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
        break;
      }
    }
  };
  
  /**
   * Constructor
   * @param _context
   * @param _act
   * @param app
   */
  public StudentAdapter(Context _context, Activity _act, MyApplication app) {
    super(_context, android.R.layout.simple_list_item_1);
    myApp = app;
    data = myApp.getStudent();
    activity = _act;
  }
  
  /**
   * Get the data item associated with the specified position in the data set.
   * @param index
   * @return data
   */
  public Student getItem(int index) {
    return this.data.get(index);
  }
  
  /**
   * Get how many items are in the data set represented by this Adapter.
   */
  public int getCount() {
    return this.data.size();
  }
  
  /**
   * Get a View that displays the data at the specified position in the data set.
   * @param position
   * @param converView
   * @param parent
   */
  public View getView(int position, View convertView, ViewGroup parent) {    
    View row = convertView;
    if (row == null) {
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row = inflater.inflate(R.layout.students_layout, parent, false);
      ImageView discard = (ImageView) row.findViewById(R.id.studentDiscard);
      discard.setOnClickListener(onClick);
      discard.setTag(position);
    }
    Student student = new Student(0, null, null, null, null, null, false, 
        null);
    student = getItem(position);
    TextView firstName = (TextView) row.findViewById(R.id.studentListFirstName);
    firstName.setText(student.getFirstName());
    firstName.setTextColor(Color.BLACK);
    TextView lastName = (TextView) row.findViewById(R.id.studentListLastName);
    lastName.setText(student.getLastName());
    lastName.setTextColor(Color.BLACK);
    TextView ageView = (TextView) row.findViewById(R.id.studentListAge);
    int age = getAge(student.getAge());
    ageView.setText("Age: "+age);
    ageView.setTextColor(Color.BLACK);
    TextView description = (TextView) row.findViewById(R.id.studentListDescription);
    description.setText(student.getDescription());
    description.setTextColor(Color.BLACK);
    ImageView iv = (ImageView) row.findViewById(R.id.studentListImage);
    if(student.getPhoto() != null){
      iv.setImageBitmap(student.getPhoto());
    }else{
      iv.setImageResource(R.drawable.ic_launcher);
    }
    return row;
  }
  
  /**
   * Get student's age by date
   * @param bday
   * @return age
   */
  private int getAge(String bday) {
    String[] date = bday.split("/");
    int day = Integer.parseInt(date[0].toString());
    int month = Integer.parseInt(date[1].toString());
    int year = Integer.parseInt(date[2].toString());
    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();
    int age = today.year - year;
    if(day < today.monthDay){
      if(!(month < today.month)){
        age = age - 1;
      }
    }else{
      if(month > today.month){
        age = age - 1;
      }
    }
    return age;
  }

  /**
   * Delete student from student's table by id
   * @param id
   */
  public void deleteStudent(long id){
    StudentSubjectTable stSbTable = new StudentSubjectTable(getContext());
    stSbTable.DeleteByStudentID(id);
  }
}




Java Source Code List

com.llenguatges.digitalcampus.BaseActivity.java
com.llenguatges.digitalcampus.MainActivity.java
com.llenguatges.digitalcampus.MyApplication.java
com.llenguatges.digitalcampus.adapters.CustomGridViewAdapter.java
com.llenguatges.digitalcampus.adapters.ExamAdapter.java
com.llenguatges.digitalcampus.adapters.NewSubSyllabusAdapter.java
com.llenguatges.digitalcampus.adapters.NewSubjectStudentAdapter.java
com.llenguatges.digitalcampus.adapters.SpinnerAdapter.java
com.llenguatges.digitalcampus.adapters.StudentAdapter.java
com.llenguatges.digitalcampus.adapters.StudentSubjectsAdapter.java
com.llenguatges.digitalcampus.adapters.SubjectAdapter.java
com.llenguatges.digitalcampus.adapters.SubjectStudentsAdapter.java
com.llenguatges.digitalcampus.adapters.SyllabusAdapter.java
com.llenguatges.digitalcampus.database.DAOHelper.java
com.llenguatges.digitalcampus.database.ExamTable.java
com.llenguatges.digitalcampus.database.StudentSubjectTable.java
com.llenguatges.digitalcampus.database.StudentTable.java
com.llenguatges.digitalcampus.database.SubjectMatterTable.java
com.llenguatges.digitalcampus.database.SubjectTable.java
com.llenguatges.digitalcampus.exams.ExamsActivity.java
com.llenguatges.digitalcampus.exams.NewExamActivity.java
com.llenguatges.digitalcampus.login.LoginActivity.java
com.llenguatges.digitalcampus.login.SessionManager.java
com.llenguatges.digitalcampus.objects.Exam.java
com.llenguatges.digitalcampus.objects.Item.java
com.llenguatges.digitalcampus.objects.StudentSubject.java
com.llenguatges.digitalcampus.objects.Student.java
com.llenguatges.digitalcampus.objects.SubjectMatter.java
com.llenguatges.digitalcampus.objects.Subject.java
com.llenguatges.digitalcampus.splash.SplashScreenActivity.java
com.llenguatges.digitalcampus.students.InformationActivity.java
com.llenguatges.digitalcampus.students.NewStudentActivity.java
com.llenguatges.digitalcampus.students.StudentsActivity.java
com.llenguatges.digitalcampus.subjects.InformationActivity.java
com.llenguatges.digitalcampus.subjects.NewSubjectActivity.java
com.llenguatges.digitalcampus.subjects.SubjectsActivity.java