我试图从SQLite数据库中获取数据,当我尝试时,我的应用程序崩溃,错误是:" android.database.CursorIndexOutOfBoundsException:索引-1请求,大小1"。 我做错了什么?
这是我的活动:
public class EventInfo extends AppCompatActivity {
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_info);
myDb=new DatabaseHelper(this);
SharedPreferences sp = getSharedPreferences("key", 0);
final String event_title=sp.getString("event_title", "");
final String event_date=sp.getString("event_date", "");
String event_content=sp.getString("event_content","");
String age_limit=sp.getString("age_limit","");
String event_hour=sp.getString("event_hour","");
String location_left=sp.getString("location_left","");
String location_right=sp.getString("location_right","");
TextView txt5=(TextView)findViewById(R.id.textView5);
TextView txt6=(TextView)findViewById(R.id.textView6);
TextView txt7=(TextView)findViewById(R.id.textView7);
TextView txt8=(TextView)findViewById(R.id.textView8);
TextView txt9=(TextView)findViewById(R.id.textView9);
TextView txt10=(TextView)findViewById(R.id.textView10);
TextView txt14=(TextView)findViewById(R.id.textView14);
txt5.setText(event_title);
txt6.setText(event_date);
txt7.setText(age_limit);
txt8.setText(event_content);
txt9.setText(event_hour);
txt10.setText("Press here for event location");
String votes_count=myDb.getVotesCount(event_title);
if(votes_count.equals("1")) {
txt14.setText("Cancel Attending");
}
else {
if(votes_count.equals("0")||votes_count.isEmpty()) {
txt14.setText("Accept Attending");
}
else
{
txt14.setText("Error");
}
}
txt10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(EventInfo.this, MapsActivity3.class);
startActivity(intent);
}
});
txt14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String votes_count=myDb.getVotesCount(event_title);
if(votes_count.equals("0")||votes_count.isEmpty()) {
boolean insert = myDb.insertData("1", event_title, event_date);
if (insert == true)
Toast.makeText(EventInfo.this, "You have accepted the arrival", Toast.LENGTH_LONG).show();
else
Toast.makeText(EventInfo.this, "Error", Toast.LENGTH_LONG).show();
}
else
{
if(votes_count.equals("1"))
{
boolean insert = myDb.insertData("0", event_title, event_date);
if (insert == true)
Toast.makeText(EventInfo.this, "You have canceled the arrival", Toast.LENGTH_LONG).show();
else
Toast.makeText(EventInfo.this, "Error, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(EventInfo.this, votes_count.toString(), Toast.LENGTH_LONG).show();
}
}
}
});
}
}
这是我的DatabaseHelper类:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="usersDB.db";
public static final String TABLE_NAME="votes_table";
public static final String COL2="VOTESCOUNT";
public static final String COL3="EVENTTITLE";
public static final String COL4="EVENTDATE";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (VOTESCOUNT TEXT, EVENTTITLE TEXT, EVENTDATE TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String votes_count, String event_title, String event_date){
SQLiteDatabase db =this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COL2,votes_count);
contentValues.put(COL3,event_title);
contentValues.put(COL4, event_date);
long result=db.insert(TABLE_NAME,null,contentValues);
if(result==-1)
{
return false;
}
else
return true;
}
public Cursor getAllData()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public Cursor getEventTitles()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res=db.rawQuery("select EVENTTITLE from "+TABLE_NAME,null);
return res;
}
public Cursor getEventDate(String eventtitle)
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res=db.rawQuery("select EVENTDATE from "+TABLE_NAME+" where EVENTTITLE='"+eventtitle+"'",null);
return res;
}
public String getVotesCount(String eventtitle)
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res=db.rawQuery("select VOTESCOUNT from "+TABLE_NAME+" where EVENTTITLE='"+eventtitle+"'",null);
String votes_count=res.getString(0);
return votes_count;
}
}
答案 0 :(得分:3)
在从中访问数据之前,必须将Cursor移动到第一行,并且检查moveToFirst()
的返回值以确保Cursor有数据是个好主意。此外,请务必在完成任务后关闭任何光标:
public String getVotesCount(String eventtitle)
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res=db.rawQuery("select VOTESCOUNT from "+TABLE_NAME+" where EVENTTITLE='"+eventtitle+"'",null);
String votes_count = null;
if (res.moveToFirst()) {
votes_count =res.getString(0);
}
res.close();
return votes_count;
}
并为返回值添加空检查:
String votes_count = myDb.getVotesCount(event_title);
if(votes_count != null && (votes_count.equals("0")||votes_count.isEmpty())) {
//...............