ListView在android上运行不正常

时间:2014-03-23 17:28:38

标签: java android sqlite listview android-listview

大家好,我的应用程序中的ListView存在问题。 我有这个添加到数据库的约会列表,然后当我想删除它们时,ListView没有向我显示约会......它只显示我'测试'。

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


}

用于管理数据库事件的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;

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

}

DeleteAppointment类:

package com.example.calendar;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DeleteAppointment extends Activity implements OnClickListener{

    DBHandler db = new DBHandler(this);
    Appointment app;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.delete);
        Button deleteAll = (Button)findViewById(R.id.btn_deleteAll);
        deleteAll.setOnClickListener(this);
        ListView list = (ListView)findViewById(R.id.apptList);
        ArrayAdapter<Appointment> mArrayAdapter = new ArrayAdapter<Appointment>(this, R.layout.item, R.id.row, db.getAppointments());
        list.setAdapter(mArrayAdapter);
        }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){  
        case R.id.btn_deleteAll:
            finish();
            break;
        }

    }

}

这里的问题是LogCat没有给我任何错误,因此,我不知道这有什么问题。 有人可以帮助我吗?

0 个答案:

没有答案