我想从我的SQLite数据库中获取两列并返回Arraylist<myObjectModel>
,但我遇到了这个错误
05-12 03:31:37.763 15166-15166/com.example.abdullah.newstroynory E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abdullah.newstroynory, PID: 15166
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abdullah.newstroynory/com.example.abdullah.newstroynory.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.abdullah.newstroynory.SQLForMyStroies.getTitleAndImage(SQLForMyStroies.java:58)
at com.example.abdullah.newstroynory.MainActivity.onCreate(MainActivity.java:75)
我的错误
com.example.abdullah.newstroynory.SQLForMyStroies.getTitleAndImage(SQLForMyStroies.java:58)
从我的SQlite类
开始forDoing.setTitle(cursor.getString(cursor.getColumnIndex("story_title")));
我的SQLiteOpenHelper类
public class SQLForMyStroies extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "newStoryNory5";
public SQLForMyStroies(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE stories (story_id INTEGER PRIMARY KEY AUTOINCREMENT,story_title VARCHAR,story_image VARCHAR,story_body VARCHAR,story_audio VARCHAR )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS stories");
onCreate(db);
}
public boolean CreateNewStory( String t, String i, String b){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("story_title" , t);
contentValues.put("story_image" , i);
contentValues.put("story_body" , b);
// contentValues.put("story_audio" , a);
db.insert("stories",null,contentValues);
return true;
}
public ArrayList<StoryModel> getTitleAndImage(){
ArrayList<StoryModel> myData = new ArrayList <> ();
SQLiteDatabase db= this.getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM stories ",null);
cursor.moveToFirst();
do {
StoryModel forDoing=new StoryModel();
forDoing.setTitle(cursor.getString(cursor.getColumnIndex("story_title")));
forDoing.setImage(cursor.getString(cursor.getColumnIndex("story_image")));
myData.add(forDoing);
}while (cursor.moveToNext());
cursor.close();
db.close();
return(myData);
}
我的MainActivity类
ArrayList<StoryModel> TitleAndImage =mySQL.getTitleAndImage();
for (StoryModel storyModel : TitleAndImage) {
Log.d("story_title", storyModel.getTitle());
Log.d("story_image", storyModel.getImage());
}
我认为它是java代码中的逻辑错误!
你能纠正我吗?我是android新手,不熟悉java
答案 0 :(得分:0)
问题是你可能没有任何行,并且你正在使用do{}while()
所以首先它会尝试始终使用第一个元素,所以它给出了CursorIndexOutOfBoundsException
您可以通过多种方式解决,例如
boolean moveToPosition(int position);
cursor.moveToFirst();
代替cursor.move(-1)
,并使用while循环。答案 1 :(得分:0)
错误是因为我的数据库SQlite为空,因为每次运行应用程序时,它都会为我创建一个新的数据库...
<强>溶液强>
所以我删除了所有的DB SQLite并且我放了条件,
我的病情是&#34; 如果我的数据库中有数据,SQLite不会从实习中获取数据&#34;
我的主要活动
if(mySQL.checktitle (storyObject.getTitle().toString())) {
Log.d("--------------------", "data is exist ");
break;
}
我的SQLiteOpenHelper类
public boolean checktitle ( String title){
SQLiteDatabase db= this.getWritableDatabase();
Cursor cur = db.rawQuery("SELECT * FROM stories where story_title='"+title+"'" ,null);
if(cur.getCount()>0)
return true;
return false;
}