2012-03-28 49 views
2

我有一个SQLite我的android应用程序的数据库设置,我有它在一个类,我可以写入信息到数据库中的工作。我的长期计划是,让我对数据库seup,并有不同类别的访问和更新数据库设置了这些类的信息:在我班上Android SQLite不能在单独的类中工作

public class DataBaseHelper extends SQLiteOpenHelper { 

public static final String TABLE_COMMENTS = "comments"; 
public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_COMMENT = "comment"; 

private static final String DATABASE_NAME = "commments.db"; 
private static final int DATABASE_VERSION = 1; 

// Database creation sql statement 
private static final String DATABASE_CREATE = "create table " 
     + TABLE_COMMENTS + "(" + COLUMN_ID 
     + " integer primary key autoincrement, " + COLUMN_COMMENT 
     + " text not null);"; 

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

@Override 
public void onCreate(SQLiteDatabase database) { 
    database.execSQL(DATABASE_CREATE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.w(DataBaseHelper.class.getName(), 
      "Upgrading database from version " + oldVersion + " to " 
        + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS); 
    onCreate(db); 
} 

}

public class Comment { 
private long id; 
private String comment; 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 

public String getComment() { 
    return comment; 
} 

public void setComment(String comment) { 
    this.comment = comment; 
} 

// Will be used by the ArrayAdapter in the ListView 
@Override 
public String toString() { 
    return comment; 
} 
} 

    public class CommentsDataSource { 

// Database fields 
private SQLiteDatabase database; 
private DataBaseHelper dbHelper; 
private String[] allColumns = { DataBaseHelper.COLUMN_ID, 
     DataBaseHelper.COLUMN_COMMENT }; 

public CommentsDataSource(Context context) { 
    dbHelper = new DataBaseHelper(context); 
} 

public void open() throws SQLException { 
    database = dbHelper.getWritableDatabase(); 
} 

public void close() { 
    dbHelper.close(); 
} 

public Comment createComment(String comment) { 
    ContentValues values = new ContentValues(); 
    values.put(DataBaseHelper.COLUMN_COMMENT, comment); 
    long insertId = database.insert(DataBaseHelper.TABLE_COMMENTS, null, 
      values); 
    Cursor cursor = database.query(DataBaseHelper.TABLE_COMMENTS, 
      allColumns, DataBaseHelper.COLUMN_ID + " = " + insertId, null, 
      null, null, null); 
    cursor.moveToFirst(); 
    Comment newComment = cursorToComment(cursor); 
    cursor.close(); 
    return newComment; 
} 

public void deleteComment(Comment comment) { 
    long id = comment.getId(); 
    System.out.println("Comment deleted with id: " + id); 
    database.delete(DataBaseHelper.TABLE_COMMENTS, DataBaseHelper.COLUMN_ID 
      + " = " + id, null); 
} 

public List<Comment> getAllComments() { 
    List<Comment> comments = new ArrayList<Comment>(); 

    Cursor cursor = database.query(DataBaseHelper.TABLE_COMMENTS, 
      allColumns, null, null, null, null, null); 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     Comment comment = cursorToComment(cursor); 
     comments.add(comment); 
     cursor.moveToNext(); 
    } 
    // Make sure to close the cursor 
    cursor.close(); 
    return comments; 
} 

private Comment cursorToComment(Cursor cursor) { 
    Comment comment = new Comment(); 
    comment.setId(cursor.getLong(0)); 
    comment.setComment(cursor.getString(1)); 
    return comment; 
} 
} 

那正在我有:

public class WorkoutProgress extends ListActivity { 
private CommentsDataSource datasource; 

@Override 

public void onCreate(Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.progress); 

    datasource = new CommentsDataSource(this); 
    datasource.open(); 

    List<Comment> values = datasource.getAllComments(); 

    // Use the SimpleCursorAdapter to show the 
    // elements in a ListView 
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, 
      android.R.layout.simple_list_item_1, values); 
    setListAdapter(adapter); 
} 

// Will be called via the onClick attribute 
// of the buttons in main.xml 
public void onClick(View view) { 
    @SuppressWarnings("unchecked") 
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter(); 
    Comment comment = null; 
    switch (view.getId()) { 
    case R.id.add: 
     String[] comments = new String[] { "Cool", "Very nice", "Hate it" }; 
     int nextInt = new Random().nextInt(3); 
     // Save the new comment to the database 
     comment = datasource.createComment(""+49); 
     adapter.add(comment); 
     break; 
    case R.id.delete: 
     if (getListAdapter().getCount() > 0) { 
      comment = (Comment) getListAdapter().getItem(0); 
      datasource.deleteComment(comment); 
      adapter.remove(comment); 
     } 
     break; 
    } 
    adapter.notifyDataSetChanged(); 
} 

@Override 
protected void onResume() { 
    datasource.open(); 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    datasource.close(); 
    super.onPause(); 
} 

} 我试图使用用于写入到数据库中不同类的代码,但它不工作的任何想法为什么呢?

+0

“不起作用”是什么意思?有错误吗?如果是这样,LogCat对此有何评论? – 2012-03-28 18:22:25

+0

我很匆忙,当我发布你的问题。我会尽力得到日志猫。发生什么事情是,当它在一个单独的类中玩时,它强制关闭应用程序 – 2012-03-28 20:24:32

回答

0

我最近回答了similar question。大概是传给你的悲伤行是这样的:

datasource = new CommentsDataSource(this); 

它可以在你的ListActivity因为Activity(这)扩展语境。只要您提供上下文,就可以使用来自任何其他类的 数据库。

+0

当我将活动更改为ListActivity时,该应用程序崩溃时,我运行该类。任何想法为什么会发生 – 2012-03-28 20:32:55

+0

它不一定是一个ListActivity,它必须*从活动(如ListActivity那样)*扩展。如果你正在接近部队,你需要发布日志猫。 – dmon 2012-03-28 20:54:44