我想将一些数据从SQLite数据库传递到RecyclerView,但不幸的是我的应用程序崩溃,我不知道如何继续实现我的目标。
目前,我的应用程序在AlertDialog中显示数据库中的元素,但我想在RecyclerView中显示它们。
这是我的代码:
以下是用户插入数据的活动:
public class InserimentoScadenze extends AppCompatActivity {
//dichiaro il db
DbHelperAdapter.ScadenzeDbHelper myDB;
EditText et1, et2, et3;
Button btnAddInfo, btnViewInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inserimento_scadenze);
/**lo istanzio, così mi richiama il costruttore nella classe
* del DbHelper così da poter creare le tabelle
*/
myDB = new DbHelperAdapter.ScadenzeDbHelper(this);
et1 = (EditText)findViewById(R.id.editTextTipo);
et2 = (EditText)findViewById(R.id.editTextNote);
et3 = (EditText)findViewById(R.id.editTextData);
btnAddInfo = (Button)findViewById(R.id.btnAdd );
btnViewInfo = (Button)findViewById(R.id.btnView);
addInfo();
viewAll();
}
public void addInfo(){
btnAddInfo.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDB.insertInfo(et1.getText().toString(),
et2.getText().toString(),
et3.getText().toString());
if(isInserted == true){
Toast.makeText(InserimentoScadenze.this, "Le informazioni sono state inserite", Toast.LENGTH_LONG).show();
et1.setText("");
et2.setText("");
et3.setText("");
} else {
Toast.makeText(InserimentoScadenze.this, "Errore durante l'inserimento" , Toast.LENGTH_LONG).show();
et1.setText("");
et2.setText("");
et3.setText("");
}
}
}
);
}
public void viewAll(){
btnViewInfo.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor result = myDB.getAllInfo();
if(result.getCount() == 0){
//mostra un messaggio
showMessage("Errore","Nessun risultato trovato");
return;
}
StringBuffer buffer = new StringBuffer();
while (result.moveToNext()){
buffer.append("Id :"+ result.getString(0)+"\n");
buffer.append("Tipo :"+ result.getString(1)+"\n");
buffer.append("Note :"+ result.getString(2)+"\n");
buffer.append("Data :"+ result.getString(3)+"\n\n ");
}
showMessage("Info",buffer.toString() );
}
}
);
}
public void showMessage(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void populateListView(){
Cursor cursor = myDB.getAllInfo();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public void onBackPressed() {
// your code.
startActivity(new Intent(this, MainActivity.class));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
这是我的DbHelperAdapter:
public class DbHelperAdapter {
static ScadenzeDbHelper helper;
public DbHelperAdapter(Context context) {
helper = new ScadenzeDbHelper(context);
}
public long insetData(String tipo, String note, String date) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ScadenzeDbHelper.COL_2, tipo);
contentValues.put(ScadenzeDbHelper.COL_3, note);
contentValues.put(ScadenzeDbHelper.COL_4, date);
long id = db.insert(ScadenzeDbHelper.TABLE_NAME, null, contentValues);
db.close();
return id;
}
public List<CardInfo> getAllData() {
SQLiteDatabase db = helper.getWritableDatabase();
List<CardInfo> list = new ArrayList<>();
String[] columns = {ScadenzeDbHelper.COL_1, ScadenzeDbHelper.COL_2, ScadenzeDbHelper.COL_3, ScadenzeDbHelper.COL_4};
Cursor cursor = db.query(ScadenzeDbHelper.TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
if(cursor != null){
while (cursor.moveToNext()) {
int cid = cursor.getInt(cursor.getColumnIndex(ScadenzeDbHelper.COL_1));
String tipo = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_2));
String note = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_3));
String date = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_4));
buffer.append(cid + " " + tipo + " " + note + " " + date + "\n");
}
}
return list;
}
public static List<CardInfo> getAllData_a() {
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {ScadenzeDbHelper.COL_1, ScadenzeDbHelper.COL_2, ScadenzeDbHelper.COL_3, ScadenzeDbHelper.COL_4};
Cursor cursor = db.query(ScadenzeDbHelper.TABLE_NAME, columns, null, null, null, null, null);
List<CardInfo> data = new ArrayList<>();
if(cursor != null){
while (cursor.moveToNext()) {
int cid = cursor.getInt(cursor.getColumnIndex(ScadenzeDbHelper.COL_1));
String tipo = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_2));
String note = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_3));
String date = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_4));
CardInfo current = new CardInfo();
current.id = cid;
current.tipo = tipo;
current.note = note;
current.date = date;
data.add(current);
}
}
return data;
}
public static String getData(String tipo) {
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {ScadenzeDbHelper.COL_2, ScadenzeDbHelper.COL_3, ScadenzeDbHelper.COL_4};
Cursor cursor = db.query(ScadenzeDbHelper.TABLE_NAME, columns, ScadenzeDbHelper.COL_2 + " = '" + tipo + "' ", null, null, null, null);
StringBuffer buffer = new StringBuffer();
if(cursor != null){
while (cursor.moveToNext()) {
String TipoNota = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_2));
String nota = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_3));
String date = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_4));
buffer.append(TipoNota + " " + nota + " " + date + "\n");
}
}
return buffer.toString();
}
static class ScadenzeDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "scadenze.db";
public static final String TABLE_NAME = "scadenze_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "TIPO";
public static final String COL_3 = "NOTE";
public static final String COL_4 = "DATA";
private static final String CREATE_TABLE="create table \" + TABLE_NAME + \"(ID INTEGER PRIMARY KEY AUTOINCREMENT, TIPO TEXT, NOTE TEXT, DATA TEXT) \"";
private Context context;
public ScadenzeDbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
/**
* il metodo qui immediatamente sotto esegue qualsiasi query gli si passi
* come parametro, accetta una String, o anche String Query
*/
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertInfo(String tipo, String note, String data) {
//costruttore
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, tipo);
contentValues.put(COL_3, note);
contentValues.put(COL_4, data);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getAllInfo() {
//costruttore
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + TABLE_NAME, null);
return result;
}
}}
这是我的Adapter类:
public class DbHelperAdapter {
static ScadenzeDbHelper helper;
public DbHelperAdapter(Context context) {
helper = new ScadenzeDbHelper(context);
}
public long insetData(String tipo, String note, String date) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ScadenzeDbHelper.COL_2, tipo);
contentValues.put(ScadenzeDbHelper.COL_3, note);
contentValues.put(ScadenzeDbHelper.COL_4, date);
long id = db.insert(ScadenzeDbHelper.TABLE_NAME, null, contentValues);
db.close();
return id;
}
public List<CardInfo> getAllData() {
SQLiteDatabase db = helper.getWritableDatabase();
List<CardInfo> list = new ArrayList<>();
String[] columns = {ScadenzeDbHelper.COL_1, ScadenzeDbHelper.COL_2, ScadenzeDbHelper.COL_3, ScadenzeDbHelper.COL_4};
Cursor cursor = db.query(ScadenzeDbHelper.TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
if(cursor != null){
while (cursor.moveToNext()) {
int cid = cursor.getInt(cursor.getColumnIndex(ScadenzeDbHelper.COL_1));
String tipo = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_2));
String note = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_3));
String date = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_4));
buffer.append(cid + " " + tipo + " " + note + " " + date + "\n");
}
}
return list;
}
public static List<CardInfo> getAllData_a() {
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {ScadenzeDbHelper.COL_1, ScadenzeDbHelper.COL_2, ScadenzeDbHelper.COL_3, ScadenzeDbHelper.COL_4};
Cursor cursor = db.query(ScadenzeDbHelper.TABLE_NAME, columns, null, null, null, null, null);
List<CardInfo> data = new ArrayList<>();
if(cursor != null){
while (cursor.moveToNext()) {
int cid = cursor.getInt(cursor.getColumnIndex(ScadenzeDbHelper.COL_1));
String tipo = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_2));
String note = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_3));
String date = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_4));
CardInfo current = new CardInfo();
current.id = cid;
current.tipo = tipo;
current.note = note;
current.date = date;
data.add(current);
}
}
return data;
}
public static String getData(String tipo) {
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {ScadenzeDbHelper.COL_2, ScadenzeDbHelper.COL_3, ScadenzeDbHelper.COL_4};
Cursor cursor = db.query(ScadenzeDbHelper.TABLE_NAME, columns, ScadenzeDbHelper.COL_2 + " = '" + tipo + "' ", null, null, null, null);
StringBuffer buffer = new StringBuffer();
if(cursor != null){
while (cursor.moveToNext()) {
String TipoNota = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_2));
String nota = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_3));
String date = cursor.getString(cursor.getColumnIndex(ScadenzeDbHelper.COL_4));
buffer.append(TipoNota + " " + nota + " " + date + "\n");
}
}
return buffer.toString();
}
static class ScadenzeDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "scadenze.db";
public static final String TABLE_NAME = "scadenze_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "TIPO";
public static final String COL_3 = "NOTE";
public static final String COL_4 = "DATA";
private static final String CREATE_TABLE="create table \" + TABLE_NAME + \"(ID INTEGER PRIMARY KEY AUTOINCREMENT, TIPO TEXT, NOTE TEXT, DATA TEXT) \"";
private Context context;
public ScadenzeDbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
/**
* il metodo qui immediatamente sotto esegue qualsiasi query gli si passi
* come parametro, accetta una String, o anche String Query
*/
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertInfo(String tipo, String note, String data) {
//costruttore
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, tipo);
contentValues.put(COL_3, note);
contentValues.put(COL_4, data);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getAllInfo() {
//costruttore
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + TABLE_NAME, null);
return result;
}
}}
这是崩溃的logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.alessandrocucci.cardrecyclerview/it.alessandrocucci.cardrecyclerview.InserimentoScadenze}: android.database.sqlite.SQLiteException: unrecognized token: """ (code 1): , while compiling: create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, TIPO TEXT, NOTE TEXT, DATA TEXT) "
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: unrecognized token: """ (code 1): , while compiling: create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, TIPO TEXT, NOTE TEXT, DATA TEXT) "
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:58
答案 0 :(得分:2)
错误发生在您的表格中:
private static final String CREATE_TABLE="create table \" + TABLE_NAME + \"(ID INTEGER PRIMARY KEY AUTOINCREMENT, TIPO TEXT, NOTE TEXT, DATA TEXT) \"";
应该是
private static final String CREATE_TABLE="CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, TIPO TEXT, NOTE TEXT, DATA TEXT)";
您以随机方式使用转义字符。