我正在尝试创建一个数据库来存储Sudoku应用程序的Sudokus。我将它们另存为字符串,稍后再将其拆分为ArrayList。 我创建了一个非常简单的活动来测试这一点,它由一个GridView组成,该GridView显示了带有适配器的Sudoku。 当我尝试插入数据库中没有的内容时,GridView可以正常工作,但是当我尝试使用数据库时,应用程序在打开时就崩溃了。我遵循了有关SQLite DB的各种教程,甚至尝试了一些老师的代码,但我真的不知道这里发生了什么。
这是我的主要活动代码:
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import static android.content.ContentValues.TAG;
import static biel.edu.fje.bdsudokuprova.SudokuDB.nomTaulaSudokus;
public class MainActivity extends AppCompatActivity {
static final String[] numbers = new String[] {
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9",
"1", "2", "3", "4", "5", "6", "7", "8", "9"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SudokuDB sudoDButil = new SudokuDB(getBaseContext());
SQLiteDatabase db = sudoDButil.getWritableDatabase(); // This is where it fails (I have to
// comment from here to line 60 for the app to at least open)
ContentValues valors = new ContentValues();
valors.put(nomTaulaSudokus, "123456789123456789123456789123456789123456789123456789123456789123456789123456789");
db.insert(nomTaulaSudokus, null, valors);
Cursor cursorSudoku = db.rawQuery("select sudoku from sudokus", null);
String sudo = null;
try {
if (cursorSudoku.moveToFirst()) {
do {
sudo = cursorSudoku.getString(1);
} while(cursorSudoku.moveToNext());
}
} catch (Exception e) {
Log.d(TAG, "Error a l'hora d'intentar agafar la informació de la base de dades");
} finally {
if (cursorSudoku != null && !cursorSudoku.isClosed()) {
cursorSudoku.close();
}
}
ArrayList<String> sudoArray = new ArrayList<String>();
for (int i = 0; i < 81; i++) {
sudoArray.add("1");
//String.valueOf(sudo.charAt(i))
}
GridView gridView = findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
编辑:我忘了提及我使用“数字”变量来测试GridView是否可以在不使用ArrayList的情况下正常工作。抱歉,有点混乱。
这是我的数据库助手类:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SudokuDB extends SQLiteOpenHelper {
public static final String nomBd = "Sudoku";
public static final int versioBd = 1;
public static final String nomTaulaSudokus = "sudokus";
public static final String columnIdSudokus = "id";
public static final String columnSudo = "sudoku";
private static final String SQL_CREACIO_TAULA_SUDOKUS = "CREATE TABLE " + nomTaulaSudokus + " ( "
+ columnIdSudokus + "INTEGER PRIMARY KEY AUTOINCREMENT,"
+ columnSudo + " TEXT )";
private static final String SQL_ESBORRAT_TAULA_SUDOKUS = "DROP TABLE IF EXISTS "
+ nomTaulaSudokus;
public SudokuDB(Context context) {
super(context, nomBd, null, versioBd);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREACIO_TAULA_SUDOKUS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_ESBORRAT_TAULA_SUDOKUS);
onCreate(db);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
非常感谢您提供的任何帮助,如果这对您所有人都造成了麻烦,我们深表歉意。这可能是一个非常简单而愚蠢的错误,但我仍然不太了解SQLite DB的工作原理。