我有一个活动,用户可以从纺纱厂填写。但是,如果用户没有填写任何此微调器,然后单击“保存”按钮,则必须显示AlertDialog。
然而,这并不像我预期的那样。当我单击“保存”按钮时,它显示错误:" CursorIndexOutOfBoundsException请求索引0,大小为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));
答案 0 :(得分:2)
请从getReport_DistrictCode
if (c != null) {
c.moveToFirst();
}
而是只返回光标,然后在Cursor rDistrict = databaseHandler.getReport_DistrictCode(District);
之后,而不是进行空检查,添加此检查if(rDistrict.moveToFirst()){ // add your logic}