2013-06-22 41 views
0

I M用發展着的應用保存在數據庫中的值?最好的辦法的Android和SQLite這裏是我的數據庫代碼請幫助我如何設置保存在數據庫中?我已經實現了功能,但dnt知道它是正確的方法,或者我可能需要使用類gettersetter保存值,並指出它如何在類中添加id屬性。什麼是採用了android和SQLite

public class DatabaseHandler extends SQLiteOpenHelper { 
private static final int db_Version = 1; 
private static final String tb_Name = "category_List"; 
private static final String tb_Name_Task = "task_List"; 

private static final String db_Name = "taskInformation"; 

public DatabaseHandler(Context context, String name, CursorFactory factory, int version) { 
    super(context, db_Name, factory, db_Version); 
} 

public void onCreate(SQLiteDatabase db) { 

    try { 
     db.execSQL(DatabaseAdapter.CREATE_TABLE); 

     db.execSQL(DatabaseAdapter.CREATE_TABLE_TASK); 
     //db.execSQL("DROP TABLE " + DatabaseAdapter.CREATE_TABLE_TASK); 
     Log.d("table message", "table created"); 
    } catch (SQLiteException e) { 
     e.printStackTrace(); 
    } 
} 

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + tb_Name); 
    db.execSQL("DROP TABLE IF EXISTS " + tb_Name_Task); 
    onCreate(db); 
} 

} databaseAdapter類別:

public class DatabaseAdapter { 

static final int db_Version = 1; 

private static final String db_Name = "taskInformation"; 

private static final String tb_Name = "category_List"; 
public static final String KEY_ID ="id"; 
public static final String KEY_CATEGORY ="category"; 

static final String CREATE_TABLE = "CREATE TABLE " + tb_Name + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CATEGORY + " TEXT NOT NULL UNIQUE" +")"; 

private static final String tb_Name_Task ="task_List"; 
public static final String KEY_TITLE ="title"; 
public static final String KEY_DESCRIPTION ="description"; 
public static final String KEY_TASKDATE ="task_Date"; 
public static final String KEY_TASKCATEGORY = "task_Category"; 
public static final String KEY_STATUS ="task_Status"; 

static final String CREATE_TABLE_TASK = "CREATE TABLE " + tb_Name_Task + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TITLE + " TEXT NOT NULL UNIQUE, " + KEY_DESCRIPTION + " TEXT NOT NULL, " + KEY_TASKCATEGORY + " TEXT NOT NULL, " + KEY_TASKDATE + " TEXT NOT NULL, " + KEY_STATUS + " INTEGER NOT NULL" +")"; 

DatabaseAdapter dataAdapter; 
public SQLiteDatabase db; 
private final Context context; 
private DatabaseHandler dbHandler; 

public DatabaseAdapter(Context _context) { 
    context = _context; 
    dbHandler = new DatabaseHandler(context,db_Name, null, db_Version); 
} 

public DatabaseAdapter openToWrite() throws SQLiteException { 
    db = dbHandler.getWritableDatabase(); 
    return this; 
} 
public DatabaseAdapter openToRead() throws SQLiteException 
{ 
    db = dbHandler.getReadableDatabase(); 
    return this; 

} 

public void close() 
{ 
    db.close(); 
} 
public SQLiteDatabase getDatabaseInstance() 
{ 
     return db; 
} 

是該方法正確的嗎?或者可以我需要通過MyClass的實例並保存使用getter和類的設置器值。

public void addTaskList(String title,String description,String date,String categorytask,Integer status) 
{ 
    ContentValues values = new ContentValues(); 
    values.put(KEY_TITLE,title); 
    values.put(KEY_DESCRIPTION,description); 
    values.put(KEY_TASKCATEGORY,categorytask); 
    values.put(KEY_TASKDATE,date); 
    values.put(KEY_STATUS,status); 
    try 
    {long val = db.insertWithOnConflict(tb_Name_Task,null,values,SQLiteDatabase.CONFLICT_IGNORE); 

    } 
    catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 

} 

請告訴我如何添加ID變量taskClass這樣,當我在列表視圖中的任何列表項點擊我可以得到該記錄的「ID」;

public class TaskClass { 

    //int task_id; //may i need to add if yes then how pls elaborate 
    int status; 
    String description; 
    String title; 
    String taskCategory; 
    String taskDate; 
    public TaskClass() { 
     //super(); 
     // TODO Auto-generated constructor stub 
    } 
    public TaskClass(int status, String description, String title, String taskCategory, String taskDate) { 

     this.status = status; 
     this.description = description; 
     this.title = title; 
     this.taskCategory = taskCategory; 
     this.taskDate = taskDate; 
    } 
    public int getStatus() { 
     return status; 
    } 
    public void setStatus(int status) { 
     this.status = status; 
    } 
    public String getDescription() { 
     return description; 
    } 
    public void setDescription(String description) { 
     this.description = description; 
    } 
    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getTaskCategory() { 
     return taskCategory; 
    } 
    public void setTaskCategory(String taskCategory) { 
     this.taskCategory = taskCategory; 
    } 
    public String getTaskDate() { 
     return taskDate; 
    } 
    public void setTaskDate(String taskDate) { 
     this.taskDate = taskDate; 
    } 
    @Override 
    public String toString() { 
     return "TaskClass [status=" + status + ", description=" + description + ", title=" + title + ", taskCategory=" + taskCategory + ", taskDate=" + taskDate + "]"; 
    } 



} 

回答

1

我看不到數據庫中您的TaskClass對象的位置。您需要實現查詢數據庫,並返回TaskClass列表供您使用的方法。然後,您可以在適配器中使用。

事情是這樣的:

public List<TaskClass> getAllTasks() { 
    List<TaskClass> tasks = new ArrayList<TaskClass>(); 
    Cursor cursor = db.query(tb_Name, null, null, null, null, null, "id asc"); 
    Log.d(TAG, "Getting tasks: " + cursor.getCount()); 
    while (cursor.moveToNext()) { 
     TaskClass task = new TaskClass(); 
     task.setId(cursor.getLong(0)); 
     task.setTitle(cursor.getString(1)); 
     // Populate rest of fields accordingly 
     tasks.add(task); 
    } 
    Log.d(TAG, "Returning tasks: " + tasks); 
    return tasks; 
} 
相關問題