gpt4 book ai didi

java - 请求获取异常索引 1,大小为 1

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:41 24 4
gpt4 key购买 nike

在我的 android 项目中从数据库中获取数据时出现异常。我试图从这行代码 String ques=c.getString(1);

中获取 column index 1 的值

这是我调用数据库类并尝试获取数据库的代码,

try{
c=db.getText(Integer.toString(subid));
if(c==null)
return;
//if(c.getCount()> 0)
//{
//int max=c.getCount();
//int min=0;

Random rn = new Random();
int randomNum = rn.nextInt(max-1) + rowid;
c.moveToPosition(randomNum);

String ques=c.getString(1);
tv.setText(ques+" ");

cans=c.getString(2);
shuffleArray(ans);
int[] a = new int[4];
int j=0;
for (int i = 0; i < ans.length; i++)
{
a[j]=ans[i];
j++;
}
ans1=c.getString(a[0]);
ans2=c.getString(a[1]);
ans3=c.getString(a[2]);
ans4=c.getString(a[3]);
rb1.setText(ans1);
rb2.setText(ans2);
rb3.setText(ans3);
rb4.setText(ans4);
}

catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage()+"exception", Toast.LENGTH_LONG).show();
}

我的数据库内容的快照,

enter image description here

这是我的数据库类,

public Cursor getText(String subId) throws SQLException //here it'll get all rows of subId=1 or whatever the value of subId
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_id, KEY_ques, KEY_correctans, KEY_wrongans1,KEY_wrongans2, KEY_wrongans3},
KEY_subjectid + "=" + " '" + subId + "' " , null,null,null, null,null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;

}

请帮助我,希望我的问题很清楚。

提前致谢。

最佳答案

你有一个错误:

Index 1 requested, with a size of 1

它说,你的游标大小是 1,你知道索引从 0 开始。因此,如果你写:

String ques = c.getString(1);

您尝试获取第二个元素,而不是第一个。如果你想得到第一个元素,你必须写:

String ques = c.getString(0);

关于java - 请求获取异常索引 1,大小为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19132043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com