Android Open Source - ProjectStudio Course Card Adapter






From Project

Back to project page ProjectStudio.

License

The source code is released under:

Apache License

If you think the Android project ProjectStudio 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 adapters;
/*from  ww w . j a  v a 2  s.c om*/
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.example.uniutilproject.R;

import java.sql.SQLException;

import DB_Provider.CourseDataSource;
import DB_Provider.DB_ABSTRACTS;
import fragments.UpdateCourseFragment;

/**
 * Created by desmond on 1/16/14.
 */
public class CourseCardAdapter extends CursorAdapter {

    private CourseDataSource dataSource;
    private Context appContext;
    private AlertDialog.Builder build;
    private long course_id;
    Cursor c;
    FragmentActivity frag_context;
    private int course_is_passed;
    private ViewGroup parent_view;

    public CourseCardAdapter(Context context, Cursor c, boolean autoRequery, ViewGroup parent) {
        super(context, c, autoRequery);
        appContext = context;
        this.c = c;
        dataSource = new CourseDataSource(context);
        frag_context = (FragmentActivity) context;
        parent_view = parent;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // when the view will be created for first time,
        // we need to tell the adapters, how each item will look
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.from(context).inflate(R.layout.course_list_item, parent, false);

        return retView;
    }


    @Override
    public void bindView(View convertView, Context context, Cursor cursor) {
        // here we are setting our data
        // that means, take the data from the cursor and put it in views
        final Handler view_handler = new Handler();

        view_handler.course_name = (TextView) convertView.findViewById(R.id.course_item_name);
        view_handler.course_room = (TextView) convertView.findViewById(R.id.course_item_room);
        view_handler.course_professor_name = (TextView) convertView.findViewById(R.id.course_item_professor);
        view_handler.course_semester = (TextView) convertView.findViewById(R.id.course_item_semester);
        view_handler.delete_button = (TextView) convertView.findViewById(R.id.course_delete_button);
        view_handler.update_button = (TextView) convertView.findViewById(R.id.course_update_button);


        //NB IT IS NECESSARY THAT THE VARIABLES ARE DECLARED AS LOCAL AND FINAL
        final long c_id = cursor.getLong(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.KEY_ID));
        final String c_name = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN));
        final String c_prof = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN));
        final String c_semester = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN));
        final String c_room = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.ROOM_COLUMN));
        final String c_score = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN));
        final String c_pass_status = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASSED_COLUMN));

        view_handler.course_professor_name.setText("Taken by Prof. " + c_prof);
        view_handler.course_name.setText(c_name);
        view_handler.course_room.setText("In room " + c_room);
        view_handler.course_semester.setText(c_semester);

        view_handler.delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("delete", "Delete Button Clicked");
                try {
                    dataSource.open();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                build = new AlertDialog.Builder(appContext);
                build.setTitle("Delete " + c_name + " ? ");
                build.setMessage("Do you really want to delete ?");
                build.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dataSource.deleteCourse(c_id);
                        Log.e("delete", c_name + " removed");
                        c = dataSource.getAllCourseData();
                        swapCursor(c);
                        notifyDataSetChanged();
                        dataSource.close();
                    }
                });

                build.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = build.create();
                alert.show();
            }
        });


        view_handler.update_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FragmentManager fm = frag_context.getSupportFragmentManager();
                UpdateCourseFragment frag = new UpdateCourseFragment();
                FragmentTransaction transaction = fm.beginTransaction();

                /*PASS THE NECESSARY DATA TO THE CALLING FRAGMENT */
                Bundle bundle = new Bundle();
                bundle.putString("professor_name", c_prof);
                bundle.putString("p_course_name", c_name);
                bundle.putString("course_room", c_room);
                bundle.putString("course_semester", c_semester);
                bundle.putLong("course_id", c_id);
                bundle.putString("course_status", c_pass_status);
                bundle.putString("course_pass_mark", c_score);

                frag.setArguments(bundle);

                // For a little polish, specify a transition animation
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                transaction.replace(parent_view.getId(), frag).addToBackStack(null).commit();

                /*
                try {
                    dataSource.open();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                course_is_passed = 0;

                build = new AlertDialog.Builder(appContext);
                build.setTitle("Update " + c_name + " ? ");
                //build.setMessage("Do you really want to delete ?");
                //Inflate a custom view
                LayoutInflater inflater = LayoutInflater.from(appContext);
                View update_view = inflater.inflate(R.layout.course_updateview, null);
                build.setView(update_view);


                final EditText course_update_name = (EditText) update_view.findViewById(R.id.course_update_name);
                final EditText course_update_room  = (EditText) update_view.findViewById(R.id.course_update_room);
                final CheckBox course_update_checkbox = (CheckBox) update_view.findViewById(R.id.course_update_passed_cb);
                final EditText course_update_score = (EditText) update_view.findViewById(R.id.course_update_passmark);
                course_update_score.setVisibility(View.INVISIBLE);

                course_update_checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (course_update_checkbox.isChecked()){
                            course_update_score.setVisibility(View.VISIBLE);
                            course_is_passed = 1;
                        } else {
                            course_update_score.setVisibility(View.GONE);
                        }
                    }
                });


                build.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        if (course_update_name.getText().toString().equals("")) {
                            Toast.makeText(appContext, "empty field", Toast.LENGTH_LONG).show();
                        } else if (course_update_room.getText().toString().equals("")) {
                            Toast.makeText(appContext, "empty field", Toast.LENGTH_LONG).show();
                        } else {
                            Course course = new Course();
                            course.setId(c_id);
                            course.setPass_mark(course_update_score.getText().toString());
                            course.setRoom(course_update_room.getText().toString());
                            course.setPassed(course_is_passed);
                            course.setSemester(c_semester);
                            Professor p = new Professor();
                            p.setProfessor_name(c_prof);
                            course.setProfessor(p);
                            course.setName(course_update_name.getText().toString());
                            dataSource.updateCourse(course);
                            c = dataSource.getAllCourseData();
                            swapCursor(c);
                            notifyDataSetChanged();
                            dataSource.close();
                        }
                    }
                });

                build.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = build.create();
                alert.show();
                */
            }

        });

    }



    //Create a holder class to contain all the view to inflate
    private static class Handler {
        TextView course_professor_name;
        TextView course_name;
        TextView course_room;
        TextView course_semester;
        TextView delete_button;
        TextView update_button;
    }
}




Java Source Code List

DB_Provider.CourseDataSource.java
DB_Provider.DBManager.java
DB_Provider.DB_ABSTRACTS.java
DB_Provider.ProfessorDataSource.java
DB_Provider.TaskDataSource.java
Drawables.DrawableAlignedButton.java
FontPackage.TypefaceSpan.java
adapters.CourseCardAdapter.java
adapters.ExamsPassedCardAdapter.java
adapters.ExamsPendingCardAdapter.java
adapters.Exams_TabsPagerAdapter.java
adapters.HomeCardAdapter.java
adapters.MatesCardAdapter.java
adapters.NavDrawerListAdapter.java
adapters.ProfessorCardAdapter.java
adapters.TaskCardAdapter.java
adapters.TasksTabPagerAdapter.java
com.example.uniutilproject.BuildConfig.java
com.example.uniutilproject.UniUtil_MainActivity.java
days_fragments.FridayTaskFragment.java
days_fragments.MondayTaskFragment.java
days_fragments.SaturdayTaskFragment.java
days_fragments.SundayTaskFragment.java
days_fragments.ThursdayTaskFragment.java
days_fragments.TuesdayTaskFragment.java
days_fragments.WednesdayTaskFragment.java
dialog_fragments.DatePickerDialogFragment.java
dialog_fragments.MatesDialogFragment.java
dialog_fragments.ProfessorsDialogFragment.java
dialog_fragments.TimePickerDialogFragment.java
fragments.AddCourseFragment.java
fragments.AddProfFragment.java
fragments.AddTaskFragment.java
fragments.CoursesFragment.java
fragments.CursorLoaderListFragment.java
fragments.ExamsFragment.java
fragments.ExamsPassedFragment.java
fragments.ExamsPendingFragment.java
fragments.HomeFragment.java
fragments.MatesFragment.java
fragments.ProfessorsFragment.java
fragments.StudyPlanFragment.java
fragments.TasksFragment.java
fragments.UpdateCourseFragment.java
fragments.UpdateProfessorFragment.java
interfaces.NoticeDialogListener.java
interfaces.package-info.java
model.Course.java
model.Days.java
model.MyParcelable.java
model.NavDrawerItem.java
model.Professor.java
model.Task.java