我试图将目标日期添加到listgDitem,将目标权重添加到listgWitem,将当前权重添加到listcWitem,并将它们插入Sqlite,并在historyActivity.java中显示它们。在settingsActivity中插入数据有效,但显示这些项目时显示以下错误:
原因:java.lang.IllegalStateException:无法读取第0行,第2行 来自CursorWindow。确保游标已正确初始化 在从中访问数据之前。 在android.database.CursorWindow.nativeGetString(本地方法) 在android.database.CursorWindow.getString(CursorWindow.java:438) 在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
在com.example.fftest.historyActivity.viewData(historyActivity.java:121)
com.example.fftest.historyActivity.onCreate(historyActivity.java:85)
historyActivity.java
public class historyActivity extends MainActivity {
private TextView mTextMessage;
Intent intent = new Intent();
ListView listView1;
ListView listView2;
ListView listView3;
ArrayList<String> listgDitem;
ArrayList<String> listgWitem;
ArrayList<String> listcWitem;
Cursor cursor;
EditText goalDinput;
EditText goalWinput;
EditText currentWinput;
DatabaseHelper myDB;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
Intent intent1 = new Intent(historyActivity.this, MainActivity.class);
startActivity(intent1);
break;
case R.id.navigation_history:
mTextMessage.setText(R.string.title_history);
Intent intent2 = new Intent(historyActivity.this, historyActivity.class);
startActivity(intent2);
break;
case R.id.navigation_progress:
mTextMessage.setText(R.string.title_progress);
Intent intent3 = new Intent(historyActivity.this, progressActivity.class);
startActivity(intent3);
break;
case R.id.navigation_settings:
mTextMessage.setText(R.string.title_settings);
Intent intent4 = new Intent(historyActivity.this, settingsActivity.class);
startActivity(intent4);
break;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
goalDinput = findViewById(R.id.goaldinput);
goalWinput = findViewById(R.id.goalwinput);
currentWinput = findViewById(R.id.currentWinput);
listView1=findViewById(R.id.goaldlistView);
listView2=findViewById(R.id.goalwlistView);
listView3=findViewById(R.id.currentwlistView);
viewData();
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text = listView1.getItemAtPosition(i).toString();
Toast.makeText(historyActivity.this,"" + text, Toast.LENGTH_SHORT).show();
}
});
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text = listView2.getItemAtPosition(i).toString();
Toast.makeText(historyActivity.this,"" + text, Toast.LENGTH_SHORT).show();
}
});
listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text = listView3.getItemAtPosition(i).toString();
Toast.makeText(historyActivity.this,"" + text, Toast.LENGTH_SHORT).show();
}
});
}
private void viewData() {
myDB = new DatabaseHelper(this);
listgDitem=new ArrayList<>();
listgWitem=new ArrayList<>();
listcWitem=new ArrayList<>();
cursor = myDB.viewData();
if (cursor.getCount() == 0) {
Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
listgDitem.add(cursor.getString(1));
listgWitem.add(cursor.getString(2));
listcWitem.add(cursor.getString(3));
}
adapter1 = new ArrayAdapter<>(this, R.layout.activity_history, listgDitem);
adapter2 = new ArrayAdapter<>(this, R.layout.activity_history, listgWitem);
adapter3 = new ArrayAdapter<>(this, R.layout.activity_history, listcWitem);
listView1.setAdapter(adapter1);
listView2.setAdapter(adapter2);
listView3.setAdapter(adapter3);
}
}
}
DatabaseHelper.java
package com.example.fftest;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DB_NAME="weightDB.db";
public static final String DB_TABLE="weightDB_data";
public static final String ID = "ID";
public static final String Col1 = "GDATE";
public static final String Col2 = "GWEIGHT";
public static final String Col3 = "CWEIGHT";
public static final String CREATE_TABLE="CREATE TABLE " + DB_TABLE + " ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
" GDATE TEXT, GWEIGHT TEXT, CWEIGHT " + ") ";
Cursor cursor;
public DatabaseHelper(Context ctx)
{
super(ctx, DB_NAME, null, 1);}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_TABLE);}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP IF TABLE EXISTS " + DB_TABLE);
onCreate(db);
}
public boolean insertData(String gDate, String gWeight, String cWeight){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col1, gDate);
contentValues.put(Col2, gWeight);
contentValues.put(Col3, cWeight);
long result = db.insert(DB_TABLE, null, contentValues);
return result !=-1;
}
public Cursor viewData(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * from " + DB_TABLE;
cursor = db.rawQuery(query,null);
return cursor;
}
}
答案 0 :(得分:0)
在您的代码中,我认为三件事可疑/错误,
1。如果您最近更改了CREATE_TABLE
模式,则必须在DatabaseHelper.java
上更新数据库版本
static final int DB_VERSION = 2;
public DatabaseHelper(Context ctx) {
super(ctx, DB_NAME, null, DB_VERSION);
}
2。找到不完整的CREATE_TABLE模式,最后一部分,在TEXT
中为CWEIGHT
键入CREATE_TABLE
。
public static final String CREATE_TABLE="CREATE TABLE " + DB_TABLE + " ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
" GDATE TEXT, GWEIGHT TEXT, CWEIGHT " + ") ";
正确的是,
public static final String CREATE_TABLE="CREATE TABLE " + DB_TABLE + " ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
" GDATE TEXT, GWEIGHT TEXT, CWEIGHT TEXT" + "); ";
3.onUpgrade()方法具有错误的架构,注意到“ IF”位置
db.execSQL("DROP IF TABLE EXISTS " + DB_TABLE);
这可能会引发如下错误,
android.database.sqlite.SQLiteException: near "IF": syntax error
正确的语法是DROP TABLE [IF EXISTS] [schema_name。] table_name; 因此,您可以使用以下代码更新代码,
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);