我试图在游标的帮助下显示存储在表格中的字符串。我从表中拉出一行但是当我执行" getString()"功能,它超出界限。
package com.example.mridul.eventmanager;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class DetailActivity extends AppCompatActivity {
DatabaseHelper db = new DatabaseHelper(this);
TextView textId,textName, textDesc, textVenue, textType;
ImageView ePic;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
textId = (TextView) findViewById(R.id.textView12);
textName = (TextView) findViewById(R.id.textView13);
textDesc = (TextView) findViewById(R.id.textView14);
textVenue = (TextView) findViewById(R.id.textView15);
textType = (TextView) findViewById(R.id.textView16);
ePic = (ImageView) findViewById(R.id.imageView2);
Cursor data = db.getRowDetail(id);
textId.setText(data.getString(0));
textName.setText(data.getString(1));
textDesc.setText(data.getString(2));
textVenue.setText(data.getString(3));
textType.setText(data.getString(4));
ePic.setImageURI(Uri.parse(data.getString(5)));
Toast.makeText(getApplicationContext(),"columns entered in list", Toast.LENGTH_SHORT).show();
}
}
游标数据取自DatabaseHelper类,该类在下面给出
public Cursor getRowDetail(int id){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM "+EVENT_TABLE_NAME+" WHERE "+EVENT_ID+" = '"+id+"'";
Cursor data = db.rawQuery(query,null);
return data;
}
id变量取自不同的类。下面给出了一小部分。
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long id) {
String event_name = adapterView.getItemAtPosition(i).toString();
Cursor data = dp.getEventId(event_name);
int itemid=-1;
while(data.moveToNext()){
itemid = data.getInt(0);
}
if( itemid >0){
detailObj.id=itemid;
Intent eventDetail = new Intent(LoginActivity.this, DetailActivity.class);
LoginActivity.this.startActivity(eventDetail);
Toast.makeText(getApplicationContext(), "Item id "+itemid, Toast.LENGTH_SHORT).show();
}
我得到的错误是......
FATAL EXCEPTION: main
Process: com.example.mridul.eventmanager, PID: 4520
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mridul.eventmanager/com.example.mridul.eventmanager.DetailActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
答案 0 :(得分:1)
在此行ID中,未定义此行的值Cursor data = db.getRowDetail(id);
。
此处int id;
这可能是一个问题
答案 1 :(得分:0)
您永远不会致电moveToFirst
,因此您无法检查结果是否为空。所以一定要检查一下:
if(data.moveToFirst()){
do{
itemid = data.getInt(0);
}while(data.moveToNext());
}
答案 2 :(得分:0)
我的SQLiteDatabase表中的一个字段为空。要使游标工作,每个字段都需要具有某种数据,否则它将无法用于检索信息。所以我填补了空场,现在它已全部固定。