gpt4 book ai didi

android - CursorIndexOutOfBoundsException 请求索引 0,在 Android 中大小为 0

转载 作者:行者123 更新时间:2023-11-29 21:39:22 24 4
gpt4 key购买 nike

我有一项 Activity ,用户可以在其中填写旋转器。但是,如果用户未填写此微调器的任何内容,然后单击“保存”按钮,则必须显示一个 AlertDialog。

然而,这并不像我想象的那样。当我点击保存按钮时,它显示错误:“CursorIndexOutOfBoundsException Index 0 requested, with a size of 0

如何解决此类错误?

DatabaseHandler.java

public Cursor getReport_DistrictCode(String district) 
{
SQLiteDatabase dbSqlite = this.getReadableDatabase();
Cursor c = dbSqlite.query(Constants.TABLE_DISTRICT, new String[]
{Constants.DISTRICT_ID, Constants.DISTRICT_CODE,Constants.DISTRICT_NAME,Constants.DISTRICT_DESCRIPTION},
Constants.DISTRICT_NAME+" = ?" , new String[]{district}, null, null, null);
if (c != null) {
c.moveToFirst();
}
dbSqlite.close();
return c;
}

MainActivity.java

spn_District.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View SelectedItem, int position, long id)
{
int rowid = (int)parentView.getItemIdAtPosition(position);
int districtId = rowid;
if(districtId == 0)
{
List<String> Province = new ArrayList<String>();
ArrayAdapter<String> adapter= new ArrayAdapter<String>(S_10th_IReportMain.this, android.R.layout.simple_spinner_item, Province);
spn_Province.setAdapter(adapter);

}

List<String> Province = databaseHandler.setItemOnProvinceSpinner(districtId);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(S_10th_IReportMain.this, R.layout.spinnerattributecell, Province) {
public View getView(int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);

Typeface externalFont=Typeface.createFromAsset(getAssets(), "Gothic_Regular.TTF");
((TextView) v).setTypeface(externalFont);

return v;
}

public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);

Typeface externalFont=Typeface.createFromAsset(getAssets(), "Gothic_Regular.TTF");
((TextView) v).setTypeface(externalFont);

return v;
}
};
adapter.setDropDownViewResource(R.layout.spinnerdropdownitem);
spn_Province.setAdapter(adapter);

}
@Override
public void onNothingSelected(AdapterView<?> arg0) {

}

});

我的 CursorAdapter 到 MainActivity.java 的实现

String District = spn_District.getSelectedItem().toString();
Cursor rDistrict = databaseHandler.getReport_DistrictCode(District);
String DistrictCode = rDistrict.getString(rDistrict.getColumnIndex(Constants.DISTRICT_CODE));

最佳答案

请从您的 getReport_DistrictCode 中删除它

if (c != null) {
c.moveToFirst();
}

相反,只需返回光标,然后在 Cursor rDistrict = databaseHandler.getReport_DistrictCode(District); 之后,不执行空检查,而是添加此检查 if(rDistrict.moveToFirst()) {//添加你的逻辑

关于android - CursorIndexOutOfBoundsException 请求索引 0,在 Android 中大小为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17539865/

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