gpt4 book ai didi

java - 如何解决错误 "android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1"

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:51 25 4
gpt4 key购买 nike

我遇到这样的问题: Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1

这是我的源代码:

public class Home extends AppCompatActivity {

JualBarangDBHelper dbcenter;
protected Cursor cursor;
String[] daftar;
ListView list;
public static Home hom;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

dbcenter = new JualBarangDBHelper(this);
hom = this;
RefreshList();
}


public void RefreshList() {

SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM jualbarang", null);
daftar = new String[cursor.getCount()];
cursor.moveToFirst();
for (int cc = 0; cc < cursor.getCount(); cc++) {
cursor.moveToPosition(cc);
daftar[cc] = cursor.getString(0).toString();
}
list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar));
list.setSelected(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();

Intent i = new Intent(getApplicationContext(), Detail.class);
i.putExtra("no", selection);
startActivity(i);
}
});
((ArrayAdapter) list.getAdapter()).notifyDataSetInvalidated();
}
}

我应该做什么?

最佳答案

首先你必须检查cursor.getCount()是否大于零,然后执行

while(cursor.movetoFirst())
{
//Your code

}

关于java - 如何解决错误 "android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37662017/

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