Android Open Source - ProjectStudio Task 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;
// w  w w.j  av a 2 s  . c  o  m
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
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 java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import DB_Provider.DB_ABSTRACTS;
import DB_Provider.TaskDataSource;

/**
 * Created by desmond on 1/28/14.
 */
public class TaskCardAdapter extends CursorAdapter {

    private Context appContext;
    private TaskDataSource dataSource;
    private AlertDialog.Builder build;
    private Cursor c;

    public TaskCardAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        this.appContext = context;
        dataSource = new TaskDataSource(context);
        this.c = c;
    }

    @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.task_listitem, 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
        Handler view_handler = new Handler();
        view_handler.task_title = (TextView) convertView.findViewById(R.id.tasklist_task_title);
        view_handler.task_note = (TextView) convertView.findViewById(R.id.tasklist_notetext);
        view_handler.task_location = (TextView) convertView.findViewById(R.id.tasklist_loactiontext);
        view_handler.task_time = (TextView) convertView.findViewById(R.id.tasklist_timetext);
        view_handler.delete_button = (TextView) convertView.findViewById(R.id.task_delete_button);

        final long task_id = cursor.getLong(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID));
        final String task_title = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TITLE_COLUMN));
        final String task_day = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN));
        view_handler.task_title.setText(task_title);
        view_handler.task_note.setText(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.NOTE_COLUMN)));
        view_handler.task_location.setText(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN)));
        String db_time = cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN));
        SimpleDateFormat timeformat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

        Date d;
        try {
            d = timeformat.parse(db_time);
            view_handler.task_time.setText(new SimpleDateFormat("HH : mm").format(d));
        } catch (ParseException e) {
            Log.e("date", "  time: " + e);
            e.printStackTrace();
        }

        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 " + task_title + " ? ");
                build.setMessage("Do you really want to delete ?");
                build.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dataSource.deleteTask(task_id);
                        Log.e("delete", task_title + " removed");

                        c = dataSource.getTaskByDay(task_day);
                        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 task_title;
        TextView task_note;
        TextView task_location;
        TextView task_time;
        TextView delete_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