Я получаю индекс из привязки при изменении списка в listview

Прежде всего, это не недопустимый индекс X, размер X тип вещи. Это немного странно, это индекс 5 размер 3, так что это не проблема для 4 ???

Я изменяю возвращенный размер в getcount()соответствии с состояниями. но getViewAt(position)получает неправильное положение.

Есть идеи, как и почему это происходит?

Я могу предоставить дополнительную информацию, если это необходимо.

Редактировать:

Я могу chage между списками, которые имеют одинаковый размер.
Также непосредственно перед ошибкой мой журнал печатает правильный размер, а затем 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 ответа

  1. Вместо Listэтого, попробуйте использовать HashMapили что-то. Я не думаю, что список предназначен для хранения индекса или ключа

  2. Делает это MudoLifeWidget.Изменение состояния между вызовами getViewAt ()? Если это так, код будет обращаться к различным коллекциям, поэтому вызовы с аргументами 4 и 5 могут не обращаться к одному и тому же ArrayList.

    Другая возможность заключается в том, что вы удаляете элементы из ArrayList между вызовами.