2012-03-28 11 views
2

私はアンドロイドアプリケーション用にSQLiteデータベースをセットアップしており、データベースに情報を書き込める1つのクラスで作業しています。私の長期計画は、私は、データベース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(); 
} 

} 私は別のクラスでデータベースへの書き込みに使用されるコードを使用しようとしましたが、それはすべてのアイデアを仕事doesntの どうして?

+0

「動作しません」とはどういう意味ですか?エラーはありましたか?もしそうなら、LogCatはそれについて何を言いましたか? –

+0

私はあなたの質問を投稿したとき私は急いでいた。私はログの猫を取得しようとします。何が起こるのは別のクラスで楽しいときに強制的にアプリケーションを閉じます –

答えて

0

私は最近非常にsimilar questionと答えました。おそらくあなたの悲しみを与えている行はこれです:

datasource = new CommentsDataSource(this); 

それはあなたのListActivityActivityので(これは)拡張コンテキストで動作します。コンテキストを提供している限り、他のクラスの データベースを使用することができます。

+0

私はクラスを実行すると、アプリケーションがクラッシュするListActivityにアクティビティを変更するときです。 –

+0

それはListActivityである必要はなく、Activityから(ListActivityのように)* extend *する必要があります。あなたは強制的に近くにいる場合は、ログの猫を投稿する必要があります。 – dmon

関連する問題