Android Open Source - digitalcampus Subject 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;
//from   www  . ja  va2  s.c  o 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.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.ExamTable;
import com.llenguatges.digitalcampus.database.StudentSubjectTable;
import com.llenguatges.digitalcampus.database.SubjectMatterTable;
import com.llenguatges.digitalcampus.database.SubjectTable;
import com.llenguatges.digitalcampus.objects.Subject;

public class SubjectAdapter extends ArrayAdapter<Subject> {
  
  private List<Subject> elements;
  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 Subject subject;
    private int pos;
    
    /**
     * Called when a view has been clicked.
     */
    public void onClick(View v) {
      pos = (Integer) v.getTag();
      subject = elements.get(pos);
      switch(v.getId()){
      case R.id.sl_discard:
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Delete");
        alertDialogBuilder.setMessage("Delete "+subject.name+"?").setCancelable(false).setPositiveButton("Delete",new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,int id) {
            SubjectTable st = new SubjectTable(getContext());
            st.DeleteById(subject.id);
            elements.remove(pos);
            deleteSubject(subject.id);
            notifyDataSetChanged();
            myApp.setSubjects(elements);
            dialog.cancel();
          }
        }).setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,int id) {
            dialog.cancel();
          }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
        break;
      }
    }
  };
  
  /**
   * Delete subject from all tables by id
   * @param id
   */
  public void deleteSubject(long id){
    SubjectMatterTable subMttTable = new SubjectMatterTable(getContext());
    subMttTable.DeleteBySubjectID(id);
    StudentSubjectTable stSbTable = new StudentSubjectTable(getContext());
    stSbTable.DeleteBySubjectID(id);
    ExamTable examTable = new ExamTable(getContext());
    examTable.DeleteBySubjectId(id);
  }
  
  /**
   * Constructor
   * @param context
   * @param act
   * @param app
   */
  public SubjectAdapter(Context context, Activity act, MyApplication app) {
    super(context, android.R.layout.simple_list_item_1);
    myApp = app;
    elements = myApp.getSubjects();
    activity = act;
  }

  /**
   * Get the data item associated with the specified position in the data set.
   * @param index
   * @return data
   */
  public Subject getItem(int index){
    return this.elements.get(index);
  }
  
  /**
   * Get how many items are in the data set represented by this Adapter.
   */
  public int getCount(){
    return this.elements.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 pos, View convertView, ViewGroup parent){
    View row = convertView;
    if(row == null){
      LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row = inflater.inflate(R.layout.subject_layout, parent, false);
      ImageView discard = (ImageView) row.findViewById(R.id.sl_discard);
      discard.setOnClickListener(onClick);
      discard.setTag(pos);
    }
    Subject subject = new Subject();
    subject = getItem(pos);
    TextView name = (TextView) row.findViewById(R.id.sl_name);
    name.setText(subject.name);
    name.setTextColor(Color.BLACK);
    TextView desc = (TextView) row.findViewById(R.id.sl_description);
    desc.setText(subject.description);
    desc.setTextColor(Color.BLACK);
    return row;
  }

}




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