androidBoomer
androidBoomer

Reputation: 3417

CursorIndexOutOfBoundsException Index 0 requested, with a size of 0 in Android

I have a activity where in the user can fill out from the spinners. But if the user didn't fill out any of this spinner and then click the Save Button, an AlertDialog must show.

However, it is not as I expected. When I click the Save Button, it shows an error: "CursorIndexOutOfBoundsException Index 0 requested, with a size of 0"

How can I resolve this kind of error?

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) {

        }

    });

Implementation of my CursorAdapter to MainActivity.java

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

Upvotes: 1

Views: 692

Answers (1)

tony m
tony m

Reputation: 4779

Please remove this from your getReport_DistrictCode

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

Instead just return the cursor and then after Cursor rDistrict = databaseHandler.getReport_DistrictCode(District); , instead of doing a null check, add this check if(rDistrict.moveToFirst()){ // add your logic}

Upvotes: 2

Related Questions