我有一個活動,用戶可以從spinners中填寫。但如果用戶沒有填寫任何這個微調,然後點擊保存按鈕,AlertDialog必須顯示。CursorIndexOutOfBoundsException索引0請求,在Android中的大小爲0
但是,它不是我所期望的。當我點擊保存按鈕,它顯示了一個錯誤:「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));
你可以給你的執行中的CursorAdapter? –
嗨@KapilVats,這是最後附上的代碼 – androidBoomer
哪條線你得到這個錯誤 –