其中哪一个是我在android上的提供者?

时间:2014-03-23 19:45:18

标签: java android sqlite android-contentprovider

我有这个简单的问题。 我在Android应用程序上使用数据库,我需要一个ContentProvider,我有两个类来管理事件和数据库中存储的数据。 这些课程是:

预约:

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;
    }


}

和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();
    }

}

LogCat告诉我它无法找到提供商。 我怎样才能解决这个问题? 请帮忙。

1 个答案:

答案 0 :(得分:1)

ContentProvider是一个Android组件。它有一个REST接口,必须是android.content.ContentProvider的子类,并且必须在Manifest中声明。 Manifest声明必须包含ContentProvide作为权限的URI。

上面显示的两个类都不能作为内容提供者使用。