我想要完成的任务:
我只是想通过SQLite存储数据。我让用户创建自己的列表。
列表存储的值是String,int和ArrayList(目前暂不使用)。
我正在处理的事情:
在我完成设置SQLite并运行我的程序后,我收到了这个错误:
android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
因此,经过一些研究后,问题是由于数据库未关闭而发生的。然而,事情就是这样:
private Context mContext;
private EasyListSQLiteHelper mEasyListSQLiteHelper;
public EasyListDataSource(Context context) {
mContext = context;
mEasyListSQLiteHelper = new EasyListSQLiteHelper(context);
}
private SQLiteDatabase open() {
return mEasyListSQLiteHelper.getWritableDatabase();
}
private void close(SQLiteDatabase database) {
database.close();
}
public ArrayList<ListItem> read() {
ArrayList<ListItem> listItems = readList();
return listItems;
}
public ArrayList<ListItem> readList() {
SQLiteDatabase database = open();
Cursor cursor = database.query(
EasyListSQLiteHelper.LISTS_TABLE,
new String[] {EasyListSQLiteHelper.LISTS_NAME, BaseColumns._ID, EasyListSQLiteHelper.LISTS_CHECKED},
null,
null,
null,
null,
null);
ArrayList<ListItem> list = new ArrayList<ListItem>();
if (cursor.moveToFirst()) {
do {
ListItem listItem = new ListItem(getIntFromColumnName(cursor, BaseColumns._ID),
getStringFromColumnName(cursor, EasyListSQLiteHelper.LISTS_NAME),
getIntFromColumnName(cursor, EasyListSQLiteHelper.LISTS_CHECKED),
null);
list.add(listItem);
}while(cursor.moveToNext());
}
cursor.close();
close(database);
return list;
}
private int getIntFromColumnName(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
return cursor.getInt(columnIndex);
}
private String getStringFromColumnName(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
return cursor.getString(columnIndex);
}
public void create(ListItem item) {
SQLiteDatabase database = open();
database.beginTransaction();
ContentValues contentValues = new ContentValues();
contentValues.put(EasyListSQLiteHelper.LISTS_NAME, item.getListName());
contentValues.put(EasyListSQLiteHelper.LISTS_CHECKED, item.getImportant());
database.insert(EasyListSQLiteHelper.LISTS_TABLE, null, contentValues);
database.setTransactionSuccessful();
database.close();
close(database);
}
如您所见,我 关闭了数据库。我不确定我在这里做错了什么。这不是我收到错误的唯一地方(评论我遇到错误的行):
的DataSource:
private Context mContext;
private EasyListSQLiteHelper mEasyListSQLiteHelper;
public EasyListDataSource(Context context) {
mContext = context;
mEasyListSQLiteHelper = new EasyListSQLiteHelper(context);
}
private SQLiteDatabase open() {
// return mEasyListSQLiteHelper.getWritableDatabase();
}
private void close(SQLiteDatabase database) {
database.close();
}
public ArrayList<ListItem> read() {
ArrayList<ListItem> listItems = readList();
return listItems;
}
public ArrayList<ListItem> readList() {
SQLiteDatabase database = open();
Cursor cursor = database.query(
EasyListSQLiteHelper.LISTS_TABLE,
new String[] {EasyListSQLiteHelper.LISTS_NAME, BaseColumns._ID, EasyListSQLiteHelper.LISTS_CHECKED},
null,
null,
null,
null,
null);
ArrayList<ListItem> list = new ArrayList<ListItem>();
if (cursor.moveToFirst()) {
do {
ListItem listItem = new ListItem(getIntFromColumnName(cursor, BaseColumns._ID),
getStringFromColumnName(cursor, EasyListSQLiteHelper.LISTS_NAME),
getIntFromColumnName(cursor, EasyListSQLiteHelper.LISTS_CHECKED),
null);
list.add(listItem);
}while(cursor.moveToNext());
}
cursor.close();
close(database);
return list;
}
private int getIntFromColumnName(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
return cursor.getInt(columnIndex);
}
private String getStringFromColumnName(Cursor cursor, String columnName) {
int columnIndex = cursor.getColumnIndex(columnName);
return cursor.getString(columnIndex);
}
public void create(ListItem item) {
// SQLiteDatabase database = open();
database.beginTransaction();
ContentValues contentValues = new ContentValues();
contentValues.put(EasyListSQLiteHelper.LISTS_NAME, item.getListName());
contentValues.put(EasyListSQLiteHelper.LISTS_CHECKED, item.getImportant());
database.insert(EasyListSQLiteHelper.LISTS_TABLE, null, contentValues);
database.setTransactionSuccessful();
database.close();
close(database);
}
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lists);
// Initialize variables...
mAddListImageView = (ImageView) findViewById(R.id.addListImageView);
mColorImageView = (ImageView) findViewById(R.id.colorImageView);
mRecyclerViewLists = (RecyclerView) findViewById(R.id.recycler_view_list);
mEmptyListTextView = (TextView) findViewById(R.id.emptyListTextView);
mRelativeLayout = (RelativeLayout) findViewById(R.id.activity_lists);
mListList = new ArrayList<>();
mOpenDeleteDialogListClickListener = this;
mRemoveListClickListener = this;
}
// Is the adapter empty?
private void checkAdapterIsEmpty() {
if (mAdapter.getItemCount() == 0) {
mEmptyListTextView.setVisibility(View.VISIBLE);
} else {
mEmptyListTextView.setVisibility(View.GONE);
}
}
// Setup RecyclerView...
protected void RecyclerViewSetup() {
mAdapter = new ListsAdapter(mOpenDeleteDialogListClickListener, mListList);
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
checkAdapterIsEmpty();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
}
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerViewLists.setLayoutManager(layoutManager);
mRecyclerViewLists.setHasFixedSize(true);
mRecyclerViewLists.setAdapter(mAdapter);
checkAdapterIsEmpty();
}
// Initializing interfaces...
@Override
public void createListClickListener(View v, String string) {
mListItem = new ListItem();
mListItem.setListName(string);
mListList.add(mListItem);
mRecyclerViewLists.scrollToPosition(mListList.size() - 1);
mAdapter.notifyDataSetChanged();
// saveList();
Toast.makeText(ListsActivity.this, "\"" + mListItem.getListName() + "\"" + " has been created.", Toast.LENGTH_SHORT).show();
}
private void saveList() {
EasyListDataSource easyListDataSource = new EasyListDataSource(this);
// easyListDataSource.create(mListItem);
}
NewItemDialogFragment:
public interface CreateListClickListener {
void createListClickListener(View v, String string);
}
public void setCreateListClickListener(CreateListClickListener createListClickListener) {
mCreateListClickListener = createListClickListener;
}
public NewListDialogFragment() {}
private EditText mListEditText;
private TextView mAcceptListTextView;
private LinearLayout mLinearLayout;
private CreateListClickListener mCreateListClickListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
// Creating the builder...
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View dialogView = getActivity().getLayoutInflater().inflate(R.layout.create_list_dialog, null);
builder.setView(dialogView);
// Initialize variables...
mListEditText = (EditText) dialogView.findViewById(R.id.listEditText);
mAcceptListTextView = (TextView) dialogView.findViewById(R.id.acceptListTextView);
mLinearLayout = (LinearLayout) dialogView.findViewById(R.id.NewListLayout);
// Creating the dialog...
final AlertDialog dialog = builder.create();
// OnClickListeners...
mAcceptListTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String item = mListEditText.getText().toString();
if (item.equals("") || item.startsWith(" ")) {
String message = "Please fill in the empty field.";
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} else {
// mCreateListClickListener.createListClickListener(view, item);
dialog.dismiss();
}
}
});
return dialog;
}
答案 0 :(得分:0)
在您的create方法中,您需要结束事务以取消阻止数据库:
public void create(ListItem item) {
// SQLiteDatabase database = open();
database.beginTransaction();
ContentValues contentValues = new ContentValues();
contentValues.put(EasyListSQLiteHelper.LISTS_NAME, item.getListName());
contentValues.put(EasyListSQLiteHelper.LISTS_CHECKED, item.getImportant());
database.insert(EasyListSQLiteHelper.LISTS_TABLE, null, contentValues);
database.setTransactionSuccessful();
database.endTransaction();
database.close();
close(database);
}