1

我有一个活动,用户可以从微调器中填写。但是,如果用户没有填写任何此微调器然后单击保存按钮,则必须显示一个 AlertDialog。

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

我该如何解决这种错误?

数据库处理程序.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));  
4

1 回答 1

2

请从您的getReport_DistrictCode

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

而是只返回游标,然后在之后Cursor rDistrict = databaseHandler.getReport_DistrictCode(District); ,而不是进行空检查,添加此检查 if(rDistrict.moveToFirst()){ // add your logic}

于 2013-07-09T05:06:16.187 回答