在Android的大小为0

在Android的大小为0

本文介绍了CursorIndexOutOfBoundsException指数0要求,在Android的大小为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动的用户在哪里可以从纺纱填写。但是,如果用户不填写任何此微调,然后点击保存按钮,一个AlertDialog必须出示。

然而,这是不是如我所料。当我点击保存按钮,它显示了一个错误: CursorIndexOutOfBoundsException指数0要求,大小为0

我怎样才能解决这种类型的错误?

DatabaseHandler.java

 公共光标getReport_District code(字符串区)
{
    SQLiteDatabase dbSqlite = this.getReadableDatabase();
    光标C = dbSqlite.query(Constants.TABLE_DISTRICT,新的String []
            {Constants.DISTRICT_ID,Constants.DISTRICT_ code,Constants.DISTRICT_NAME,Constants.DISTRICT_DESCRIPTION},
            Constants.DISTRICT_NAME +=? ,新的String [] {}区,NULL,NULL,NULL);
    如果(C!= NULL){
        c.moveToFirst();
    }
    dbSqlite.close();
    返回℃;
}

MainActivity.java

  spn_District.setOnItemSelectedListener(新OnItemSelectedListener(){
    @覆盖
        公共无效onItemSelected(适配器视图<> parentView,查看的SelectedItem,INT位置,长的ID)
        {
            INT ROWID =(int)的parentView.getItemIdAtPosition(位置);
            INT districtId = ROWID;
            如果(districtId == 0)
            {
                清单<串GT;省=新的ArrayList<串GT;();
                ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(S_10th_IReportMain.this,android.R.layout.simple_spinner_item,省);
                spn_Province.setAdapter(适配器);            }            清单<串GT;省= databaseHandler.setItemOnProvinceSpinner(districtId);
            ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(S_10th_IReportMain.this,R.layout.spinnerattributecell,省){
                公共查看getView(INT位置,查看convertView,父母的ViewGroup)
                {
                    视图V = super.getView(位置,convertView,父母);                    字体externalFont = Typeface.createFromAsset(getAssets(),Gothic_Regular.TTF);
                    ((的TextView)V).setTypeface(externalFont);                    返回伏;
                }                公共查看getDropDownView(INT位置,查看convertView,父母的ViewGroup){
                      视图V = super.getDropDownView(位置,convertView,父母);                     字体externalFont = Typeface.createFromAsset(getAssets(),Gothic_Regular.TTF);
                     ((的TextView)V).setTypeface(externalFont);                     返回伏;
                }
            };
            adapter.setDropDownViewResource(R.layout.spinnerdropdownitem);
            spn_Province.setAdapter(适配器);        }
        @覆盖
        公共无效onNothingSelected(适配器视图<>为arg0){        }    });

我的CursorAdapter的执行情况MainActivity.java

 字符串区= spn_District.getSelectedItem()的toString()。
光标rDistrict = databaseHandler.getReport_District code(区);
字符串区code = rDistrict.getString(rDistrict.getColumnIndex(Constants.DISTRICT_ code));


解决方案

请从删除此你的 getReport_District code

 如果(C!= NULL){
        c.moveToFirst();
    }

而不是仅仅返回光标,然后光标rDistrict = databaseHandler.getReport_District code(区)后,中,而不是做一个空检查,添加此检查如果(rDistrict.moveToFirst()){//添加你的逻辑}

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));
解决方案

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}

这篇关于CursorIndexOutOfBoundsException指数0要求,在Android的大小为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:32