Question

I have this simple question. I'm working with a database on an android application and I need a ContentProvider for it and I have this two classes that manage the events and the data stored in the database. These classes are:

Appointment:

package com.example.calendar;


public class Appointment {

    //variables(that are the attributes of the database table)
    int _id;
    String _title;
    String _time;
    String _details;
    String _date;

    //empty constructor to add for the update method in the DBHandler
    public Appointment(){

    }

    public Appointment(int id, String date, String title, String time, String details){

        this._id = id;
        this._date = date;
        this._title = title;
        this._time = time;
        this._details = details;

    }

    public Appointment(String date, String title, String time, String details){

        this._date = date;
        this._title = title;
        this._time = time;
        this._details = details;

    }

    //----------GET/SET METHODS BELOW-----------

    //--------ID---------

    public int getID(){
        return this._id;
    }

    public void setID(int id){
        this._id = id;
    }

    //-------DATE-------

    public String getDate(){
        return this._date;
    }

    public void setDate(String date){
        this._date = date;
    }

    //-----TITLE---------

    public String getTitle(){
        return this._title;
    }

    public void setTitle(String title){
        this._title = title;
    }

    //------TIME-------

    public String getTime(){
        return this._time;
    }

    public void setTime(String time){
        this._time = time;
    }

    //------DETAILS--------

    public String getDetails(){
        return this._details;
    }

    public void setDetails(String details){
        this._details = details;
    }

    @Override
    public String toString() {
        return _title;
    }


}

And DBHandler:

package com.example.calendar;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class DBHandler extends SQLiteOpenHelper {

    //Variables needed
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "appts";// database name
    private static final String TABLE_APP = "appointments";// table name
    public static final String AUTHORITY = "com.example.calndar";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_APP);
    //key names for the table
    private static final String KEY_ID = "id";
    private static final String KEY_DATE = "date";
    private static final String KEY_TITLE = "title";
    private static final String KEY_TIME = "time";
    private static final String KEY_DETAILS = "details";
    private static String SEARCH = "";

    public DBHandler(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override//create table
    public void onCreate(SQLiteDatabase db){
        String CREATE_APPOINTMENTS_TABLE = "CREATE TABLE " + TABLE_APP + "("
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_DATE + " TEXT NOT NULL,"
                + KEY_TITLE + " TEXT, " + KEY_TIME + " TEXT, " + KEY_DETAILS + " TEXT" + ")";
        db.execSQL(CREATE_APPOINTMENTS_TABLE);
    }

    @Override//when upgraded, trigger
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_APP);//delete table from older version
        onCreate(db);//create new table
    }

    void addAppointment(Appointment appointment){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_DATE, appointment.getDate().toString());
        values.put(KEY_TITLE, appointment.getTitle());
        values.put(KEY_TIME, appointment.getTime());
        values.put(KEY_DETAILS, appointment.getDetails());

        db.insert(TABLE_APP, null, values);
        db.close();

    }

    Appointment getAppointment(int id){
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_APP, new String[] {KEY_ID, KEY_DATE, KEY_TITLE, KEY_TIME, KEY_DETAILS}, KEY_ID + "=?", 
                new String[]{String.valueOf(id)}, null, null, null, null);
        if(cursor != null){
            cursor.moveToFirst();
        }
        Appointment appointment = new Appointment(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
        return appointment;
    }

    public List<Appointment> getAppointments(){
        List<Appointment> appointmentList = new ArrayList<Appointment>();
        String selectQuery = "SELECT * FROM " + TABLE_APP;//query to search appointment by title
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if(cursor.moveToFirst()){
            do{
                Appointment appointment = new Appointment();
                //appointment.setID(Integer.parseInt(cursor.getString(0)));
                appointment.setDate(cursor.getString(1));
                appointment.setTitle(cursor.getString(2));
                appointment.setTime(cursor.getString(3));
                appointment.setDetails(cursor.getString(4));

                appointmentList.add(appointment);
            } while(cursor.moveToNext());
        }

        return appointmentList;
    }

    public int updateAppointment(Appointment appointment){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_DATE, appointment.getDate());
        values.put(KEY_TITLE, appointment.getTitle());
        values.put(KEY_TIME, appointment.getTime());
        values.put(KEY_DETAILS, appointment.getDetails());

        return db.update(TABLE_APP, values, KEY_ID + " = ?", new String[] {String.valueOf(appointment.getID())});
    }

    public void deleteAppointment(Appointment appointment){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_APP, KEY_ID + " = ?", new String[]{String.valueOf(appointment.getID())});
        db.close();
    }

    public int getAppointmentCount(){
        String countQuery = "SELECT * FROM " + TABLE_APP;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        return cursor.getCount();
    }

}

And LogCat is telling me that it can't find a provider. How can i fix this? Please help.

Was it helpful?

Solution

A ContentProvider is an Android component. It has a REST interface, must be a subclass of android.content.ContentProvider and must be declared in the Manifest. The Manifest declaration must include the URI for which the ContentProvide is the authority.

Neither of the two classes that you show, above, will work as a content provider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top