Back to project page digitalcampus.
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.
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; } }