在listview中更改列表时,我得到索引越界

时间:2016-05-03 05:42:56

标签: android listview indexoutofboundsexception android-appwidget

首先它不是索引X无效,大小X类型的东西。这有点奇怪是索引5大小3所以它不是4 ???

的问题

我根据状态更改了getcount()中的返回大小。但getViewAt(position)的位置错误。

任何想法如何或为何发生这种情况?

如果需要,我可以提供额外的信息。

编辑:

我可以在相同大小的列表之间切换。 也就是在错误之前我的日志打印正确的大小然后getViewAt被调用4次并且在5时被致命

E/AndroidRuntime: FATAL EXCEPTION: Binder_3
              Process: com.mudolife.kubilayaydin.mudolifeapp, PID: 25022
              java.lang.IndexOutOfBoundsException: Invalid index 5, size is 3
                  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                  at java.util.ArrayList.get(ArrayList.java:308)
                  at com.mudolife.kubilayaydin.mudolifeapp.WidgetDataProvider.getViewAt(WidgetDataProvider.java:120)
                  at android.widget.RemoteViewsService$RemoteViewsFactoryAdapter.getViewAt(RemoteViewsService.java:164)
                  at com.android.internal.widget.IRemoteViewsFactory$Stub.onTransact(IRemoteViewsFactory.java:85)
                  at android.os.Binder.execTransact(Binder.java:446)

这是WidgetDataProvider:

@SuppressLint("NewApi")
public class WidgetDataProvider implements RemoteViewsFactory {

    SharedPreferences sharedPreferences;
    String phonenumber;
    String personelno;
    int mAppWidgetId;
    ArrayList<Anlik> mCollections = new ArrayList<Anlik>();
    ArrayList<Anlik> mSatıs = new ArrayList<Anlik>();
    ArrayList<Anlik> mSatıs_Concept = new ArrayList<Anlik>();
    ArrayList<Anlik> mSatıs_HazırG = new ArrayList<Anlik>();
    ArrayList<Anlik> mYeniSezon = new ArrayList<Anlik>();

    Context mContext;
    //Intent mIntent=null;
    public WidgetDataProvider(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    @Override
    public int getCount() {
        switch (MudoLifeWidget.State){
            case Satis:
                Log.d("***SIZE***", "getCount: "+mSatıs.size());
                return mSatıs.size();
            case Satis_Concept:
                Log.d("***SIZE***", "getCount: "+mSatıs_Concept.size());
                return mSatıs_Concept.size();
            case Satis_HazirG:
                Log.d("***SIZE***", "getCount: "+mSatıs_HazırG.size());
                return mSatıs_HazırG.size();
            case Satis_Toplam:
                Log.d("***SIZE***", "getCount: "+mSatıs.size());
                return mSatıs.size();
            case YSezon:
                Log.d("***SIZE***", "getCount: "+mYeniSezon.size());
                return mYeniSezon.size();
            case Marj:
                Log.d("***SIZE***", "getCount: "+mSatıs.size());
                return mSatıs.size();
            case Marj_Concept:
                Log.d("***SIZE***", "getCount: "+mSatıs_Concept.size());
                return mSatıs_Concept.size();
            case Marj_HazirG:
                Log.d("***SIZE***", "getCount: "+mSatıs_HazırG.size());
                return mSatıs_HazırG.size();
            case Marj_Toplam:
                Log.d("***SIZE***", "getCount: "+mSatıs.size());
                return mSatıs.size();
            case Donusum:
                Log.d("***SIZE***", "getCount: "+mCollections.size());
                return mCollections.size();

        }
        return 10;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        String lside,rside,ttr,ttrgy;
        switch (MudoLifeWidget.State){
            case Satis:
                RemoteViews mViewCat1 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                lside=mSatıs.get(position).getTtr().substring(0,mSatıs.get(position).getTtr().length()-3);
                rside=mSatıs.get(position).getTtr().substring(mSatıs.get(position).getTtr().length()-3, mSatıs.get(position).getTtr().length());
                ttr=lside+","+rside;
                lside=mSatıs.get(position).getTtrGY().substring(0,mSatıs.get(position).getTtrGY().length()-3);
                rside=mSatıs.get(position).getTtrGY().substring(mSatıs.get(position).getTtrGY().length()-3, mSatıs.get(position).getTtrGY().length());
                ttrgy=lside+","+rside;
                mViewCat1.setTextViewText(R.id.tag, mSatıs.get(position).getUrnGrp());
                mViewCat1.setTextViewText(R.id.data1,ttr);
                mViewCat1.setTextViewText(R.id.data2, ttrgy);
                mViewCat1.setTextViewText(R.id.data3, mSatıs.get(position).getDegisim()+"%");
                Log.d("***BTN1***", "getViewAt: ");
                return mViewCat1;
            case YSezon:
                RemoteViews mViewCat2 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat2.setTextViewText(R.id.tag, mYeniSezon.get(position).getUrnGrp());
                mViewCat2.setTextViewText(R.id.data1, mYeniSezon.get(position).getTtr());
                mViewCat2.setTextViewText(R.id.data2, mYeniSezon.get(position).getTtrGY());
                mViewCat2.setTextViewText(R.id.data3, mYeniSezon.get(position).getDegisim()+"%");
                Log.d("***BTN2***", "getViewAt: ");
                return mViewCat2;
            case Marj:
                RemoteViews mViewCat3 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat3.setTextViewText(R.id.tag, mSatıs.get(position).getUrnGrp());
                mViewCat3.setTextViewText(R.id.data1, mSatıs.get(position).getMarj()+"%");
                mViewCat3.setTextViewText(R.id.data2, mSatıs.get(position).getMarjGY()+"%");
                mViewCat3.setTextViewText(R.id.data3, mSatıs.get(position).getMarjDegisim()+"%");
                Log.d("***BTN3***", "getViewAt: ");
                return mViewCat3;
            case Marj_Concept:
                RemoteViews mViewCat31 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat31.setTextViewText(R.id.tag, mSatıs_Concept.get(position).getUrnGrp());
                mViewCat31.setTextViewText(R.id.data1, mSatıs_Concept.get(position).getMarj());
                mViewCat31.setTextViewText(R.id.data2, mSatıs_Concept.get(position).getMarjGY());
                mViewCat31.setTextViewText(R.id.data3, mSatıs_Concept.get(position).getMarjDegisim());
                Log.d("***BTN3***", "getViewAt: ");
                return mViewCat31;
            case Marj_HazirG:
                RemoteViews mViewCat32 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat32.setTextViewText(R.id.tag, mSatıs_HazırG.get(position).getUrnGrp());
                mViewCat32.setTextViewText(R.id.data1, mSatıs_HazırG.get(position).getMarj());
                mViewCat32.setTextViewText(R.id.data2, mSatıs_HazırG.get(position).getMarjGY());
                mViewCat32.setTextViewText(R.id.data3, mSatıs_HazırG.get(position).getMarjDegisim());
                Log.d("***BTN3***", "getViewAt: ");
                return mViewCat32;
            case Marj_Toplam:
                RemoteViews mViewCat33 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat33.setTextViewText(R.id.tag, mSatıs.get(position).getUrnGrp());
                mViewCat33.setTextViewText(R.id.data1, mSatıs.get(position).getMarj()+"%");
                mViewCat33.setTextViewText(R.id.data2, mSatıs.get(position).getMarjGY()+"%");
                mViewCat33.setTextViewText(R.id.data3, mSatıs.get(position).getMarjDegisim()+"%");
                Log.d("***BTN3***", "getViewAt: ");
                return mViewCat33;

            case Donusum:
                RemoteViews mViewCat4 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat4.setTextViewText(R.id.tag, mCollections.get(position).getUrnGrp() + mCollections.get(position).getMarjResim());
                Log.d("***BTN4***", "getViewAt: ");
                return mViewCat4;
            case Satis_Concept:
                RemoteViews mViewCat5 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat5.setTextViewText(R.id.tag, mSatıs_Concept.get(position).getUrnGrp());
                mViewCat5.setTextViewText(R.id.data1, mSatıs_Concept.get(position).getTtr());
                mViewCat5.setTextViewText(R.id.data2, mSatıs_Concept.get(position).getTtrGY());
                mViewCat5.setTextViewText(R.id.data3, mSatıs_Concept.get(position).getDegisim()+"%");
                Log.d("***BTN1.1***", "getViewAt: ");
                return mViewCat5;
            case Satis_HazirG:
                RemoteViews mViewCat6 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                mViewCat6.setTextViewText(R.id.tag, mSatıs_HazırG.get(position).getUrnGrp());
                mViewCat6.setTextViewText(R.id.data1, mSatıs_HazırG.get(position).getTtr());
                mViewCat6.setTextViewText(R.id.data2, mSatıs_HazırG.get(position).getTtrGY());
                mViewCat6.setTextViewText(R.id.data3, mSatıs_HazırG.get(position).getDegisim()+"%");
                Log.d("***BTN1.2***", "getViewAt: ");
                return mViewCat6;
            case Satis_Toplam:
                RemoteViews mViewCat7 = new RemoteViews(mContext.getPackageName(),R.layout.rows_widget);
                lside=mSatıs.get(position).getTtr().substring(0,mSatıs.get(position).getTtr().length()-3);
                rside=mSatıs.get(position).getTtr().substring(mSatıs.get(position).getTtr().length()-3, mSatıs.get(position).getTtr().length());
                ttr=lside+","+rside;
                lside=mSatıs.get(position).getTtrGY().substring(0,mSatıs.get(position).getTtrGY().length()-3);
                rside=mSatıs.get(position).getTtrGY().substring(mSatıs.get(position).getTtrGY().length()-3, mSatıs.get(position).getTtrGY().length());
                ttrgy=lside+","+rside;
                mViewCat7.setTextViewText(R.id.tag, mSatıs.get(position).getUrnGrp());
                mViewCat7.setTextViewText(R.id.data1,ttr);
                mViewCat7.setTextViewText(R.id.data2,ttrgy);
                mViewCat7.setTextViewText(R.id.data3, mSatıs.get(position).getDegisim()+"%");
                Log.d("***BTN1.3***", "getViewAt: ");
                return mViewCat7;
            default:
                RemoteViews mView = new RemoteViews(mContext.getPackageName(), R.layout.rows_widget);
                mView.setTextViewText(R.id.appwidget_text, mCollections.get(position).getMarjGY() + mCollections.get(position).getMarj());
                mView.setTextColor(R.id.appwidget_text, Color.BLACK);
                Log.d("***ELSE***", "getViewAt: ");
                return mView;
        }

    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onCreate() {
        mCollections.clear();
        sharedPreferences = mContext.getSharedPreferences("PREFERENCES", mContext.getApplicationContext().MODE_PRIVATE);
        phonenumber = sharedPreferences.getString("mPhonenumber", "");
        personelno = sharedPreferences.getString("mPersonalno", "");
        class AsyncAnlikCheck extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                anlikService(phonenumber, personelno);
                return null;
            }
        }
        AsyncAnlikCheck asyncanlikcheck = new AsyncAnlikCheck();

        try {
            Void result = asyncanlikcheck.execute().get();
            Log.d("****CollectionSize****", "initDataFromService_else: "+mCollections.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onDataSetChanged() {
        Log.d("**********", "onDataSetChanged: "+"**********************");
    }

    private void initDataFromService() {
        mCollections.clear();

        sharedPreferences = mContext.getSharedPreferences("PREFERENCES", mContext.getApplicationContext().MODE_PRIVATE);
        phonenumber = sharedPreferences.getString("mPhonenumber", "");
        personelno = sharedPreferences.getString("mPersonalno", "");
        class AsyncAnlikCheck extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                anlikService(phonenumber, personelno);
                return null;
            }
        }
        AsyncAnlikCheck asyncanlikcheck = new AsyncAnlikCheck();
        try {
            Void result = asyncanlikcheck.execute().get();
            Log.d("****CollectionSize****", "initDataFromService_else: "+mCollections.size());
            Log.d("****CollectionSize****", "initDataFromService_else: "+mSatıs.size());
            Log.d("****CollectionSize****", "initDataFromService_else: "+mSatıs_Concept.size());
            Log.d("****CollectionSize****", "initDataFromService_else: "+mSatıs_HazırG.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }


    public void anlikService(String phonenumber,String personalno){
        SoapObject request = new SoapObject(LoginActivity.NAMESPACE,"MudoLife_getAnlikSatisAuth");
        int ConceptBugun=0;
        int marjConceptBugun=0;
        int ConceptGY=0;
        int marjConceptGY=0;
        int ConceptDegisim=0;
        int marjConceptDegisim=0;
        int HGBugun=0;
        int marjHGBugun=0;
        int HGGY=0;
        int marjHGGY=0;
        int HGDegisim=0;
        int marjHGDegisim=0;
        int TotalBugun=0;
        int TotalGY=0;
        int TotalDegisim=0;
        int marjTotalDegisim=0;
        request.addProperty("phoneNumber",phonenumber);
        request.addProperty("personelNumber",personalno);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE transportSE = new HttpTransportSE(LoginActivity.URL);

        transportSE.debug=true;

        try{

            transportSE.call("http://tempuri.org/MudoLife_getAnlikSatisAuth",envelope);
            final SoapObject response = (SoapObject) envelope.getResponse();
            if(response.getPropertyCount()!=0){

                for(int i=0;i<response.getPropertyCount();i++){
                    SoapObject anliklistt = (SoapObject) response.getProperty(i);

                    mCollections.add(new Anlik(anliklistt.getProperty("urnGrp").toString(),
                            anliklistt.getProperty("ttr").toString(),
                            anliklistt.getProperty("ttrGY").toString(),
                            anliklistt.getProperty("degisim").toString(),
                            anliklistt.getProperty("resim").toString(),
                            anliklistt.getProperty("marj").toString(),
                            anliklistt.getProperty("marjGY").toString(),
                            anliklistt.getProperty("marjDegisim").toString(),
                            anliklistt.getProperty("marjResim").toString()));


                    if(anliklistt.getProperty("urnGrp").toString().startsWith("001")){
                        if(!anliklistt.getProperty("urnGrp").toString().endsWith("001")||
                                !anliklistt.getProperty("urnGrp").toString().endsWith("008")||
                                !anliklistt.getProperty("urnGrp").toString().endsWith("009")||
                                !anliklistt.getProperty("urnGrp").toString().endsWith("010")){
                            switch (anliklistt.getProperty("urnGrp").toString()){
                                case "001002":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                                case "001003":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                                case "001004":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                                case "001005":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                                case "001006":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                                case "001007":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                                case "001011":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                                case "001012":
                                    mSatıs_Concept.add(new Anlik());

                                    break;
                            }
                        }
                    }

                    if(anliklistt.getProperty("urnGrp").toString().startsWith("002")){
                            switch (anliklistt.getProperty("urnGrp").toString()){
                                case "002001":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002002":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002003":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002004":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002011":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002012":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002013":

                                    break;
                                case "002014":

                                    break;
                                case "002015":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002016":
                                    mSatıs_HazırG.add(new Anlik());

                                    break;
                                case "002017":

                                    break;
                                case "002018":

                                    break;
                                case "002019":

                                    break;
                                case "002020":

                                    break;
                                case "002021":

                                    break;
                                case "002022":

                                    break;

                            }

                    }
                }
                marjConceptDegisim=(marjConceptBugun-marjConceptGY)*100/marjConceptGY;
                ConceptDegisim=(ConceptBugun-ConceptGY)*100/ConceptGY;
                HGDegisim=(HGBugun-HGGY)*100/HGGY;
                marjHGDegisim=(marjHGBugun-marjHGGY)*100/marjHGGY;
                TotalDegisim=((ConceptBugun+HGBugun)-(ConceptGY+HGGY))*100/(ConceptGY+HGGY);
                marjTotalDegisim=((marjConceptBugun+HGBugun)-(marjConceptGY+HGGY))*100/(marjConceptGY+HGGY);
                mSatıs.add(new Anlik("CONCEPT",Integer.toString(ConceptBugun),Integer.toString(ConceptGY),Integer.toString(ConceptDegisim),"",Integer.toString(marjConceptBugun),Integer.toString(marjConceptGY),Integer.toString(marjConceptDegisim),""));
                mSatıs.add(new Anlik("HAZIR GIYIM",Integer.toString(HGBugun),Integer.toString(HGGY),Integer.toString(HGDegisim),"",Integer.toString(marjHGBugun),Integer.toString(marjHGGY),Integer.toString(marjHGDegisim),""));
                mSatıs.add(new Anlik("TOTAL", Integer.toString(ConceptBugun + HGBugun), Integer.toString(ConceptGY + HGGY), Integer.toString(TotalDegisim), "", Integer.toString(marjConceptBugun + marjHGBugun), Integer.toString(marjConceptGY + marjHGGY), Integer.toString(marjTotalDegisim), ""));

            }
            mYeniSezon.addAll(mSatıs_HazırG);
            mYeniSezon.remove(mYeniSezon.size() - 1);
            mYeniSezon.remove(mYeniSezon.size() - 1);

        }catch (Exception e){
            e.printStackTrace();
        }
    }



    @Override
    public void onDestroy() {

    }

}

2 个答案:

答案 0 :(得分:0)

而不是List,请尝试使用HashMap或其他内容。我不认为List旨在保持索引或键

答案 1 :(得分:0)

你的getViewAt()调用之间是否有MudoLifeWidget.State的变化?如果是,代码将访问不同的集合,因此使用4和5作为参数的调用可能无法访问相同的ArrayList。

另一种可能性是你在两次调用之间从ArrayList中删除项目。