Android java.lang.IndexOutOfBoundsException:索引0无效,大小为0

时间:2016-02-02 18:53:35

标签: java android

我有一个ToDo应用程序,我需要对两个ArrayLists进行排序 - 首先使用Strings(用户指定的条目名称),然后使用Bitmaps(用户添加的图像作为条目附件)。如果我使用字符串对arraylist进行排序,我也必须使用位图对数组进行排序,因为位图必须与字符串匹配。 第一个arraylist(Strings)的排序工作得很好但是第二个arraylist(Bitmasp)没有。我收到此错误:

  

致命的例外:主要                                                      处理:com.example.administrator.myapplication,PID:23690                                                      java.lang.IndexOutOfBoundsException:索引0无效,大小为0                                                          at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)                                                          at java.util.ArrayList.get(ArrayList.java:308)                                                          在com.example.administrator.myapplication.CustomAdapter.getView(CustomAdapter.java:67)                                                          在android.widget.AbsListView.obtainView(AbsListView.java:2346)                                                          在android.widget.ListView.measureHeightOfChildren(ListView.java:1280)                                                          在android.widget.ListView.onMeasure(ListView.java:1188)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                          在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)                                                          在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)                                                          在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                          在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)                                                          在android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:78)                                                          在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1132)                                                          在android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:675)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                          在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)                                                          在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                          在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)                                                          在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)                                                          在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                          在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                          在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)                                                          在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)                                                          在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                          在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)                                                          在com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643)                                                          在android.view.View.measure(View.java:18788)                                                          在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)                                                          在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)                                                          在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)                                                          在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)                                                          在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013)                                                          在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)                                                          在android.view.Choreographer.doCallbacks(Choreographer.java:670)                                                          在android.view.Choreographer.doFrame(Choreographer.java:606)                                                          在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)                                                          在android.os.Handler.handleCallback(Handler.java:739)                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                          在android.os.Looper.loop(Looper.java:148)                                                          在android.app.ActivityThread.main(ActivityThread.java:5417)                                                          at java.lang.reflect.Method.invoke(Native Method)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

private void Sort(String param){


    Toast t2 =  Toast.makeText(MainActivity.this, "You selected : " + param, Toast.LENGTH_LONG);
   // t2.show();
    String t;

    items2.clear();




    switch(param) {

        case "Nazwa":
            Collections.sort(events, Zdarzenie.SortNazwa);
            break;

        case "Data dodania":
            Collections.sort(events, Zdarzenie.SortDataDod);
            break;

        case "Data zakończenia":
            Collections.sort(events, Zdarzenie.SortData);
            break;

        case "Priorytet":
            Collections.sort(events, Zdarzenie.SortPriorytet);
            break;
        case "Status":
            Collections.sort(events, Zdarzenie.SortStatus);
            break;

    }
    for (int i = 0; i < events.size(); i++) {
        Zdarzenie z = events.get(i);

          if(param.equalsIgnoreCase("Nazwa")){
              t = z.getNazwa() + " " + z.getGodzina() + " " + z.getData() + " " + z.getData() + " " + z.getGodzina();
              items2.add(t);
          }
         if(param.equalsIgnoreCase("Data dodania")){
             t = z.getData_dodania() + " " + z.getGodzina_dodania() + " " + z.getNazwa() + " " + z.getData() + " " + z.getGodzina();
             items2.add(t);
         }
        if(param.equalsIgnoreCase("Data zakończenia")){
            t = z.getData() + " " + z.getGodzina() + " " + z.getNazwa() + " " + z.getData() + " " + z.getGodzina();
            items2.add(t);
        }
        if(param.equalsIgnoreCase("Priorytet")){
            t = z.getPriorytet() + " " + z.getNazwa() + " " + z.getData() + " " + z.getGodzina();
            items2.add(t);
        }
        if(param.equalsIgnoreCase("Status")){
            if (z.isAktywny() == true) {
                t = "Aktywny" + " " + z.getNazwa() + " " + z.getData() + " " + z.getGodzina();
                items2.add(t);
            }
            if (z.isAktywny() == false) {
                t = "Nieaktywny" + " " + z.getNazwa() + " " + z.getData() + " " + z.getGodzina();
                items2.add(t);
            }

        }


    }

    //itemsAdapter2 = new CustomAdapter(this, items2,images2);



    int is = itemsAdapter.getCount();

    Toast t3 =  Toast.makeText(MainActivity.this, "Items2.size : " + is, Toast.LENGTH_LONG);
    t3.show();

           String tmp1,tmp2;

    for(int i =0 ; i<is ; i++){

        for(int j=0; j<is; j++){

            itemsAdapter.getItem(i);
         //   images.get(i);
           // itemsAdapter2.getItem(j);
            items2.get(j);
           // if(itemsAdapter.getItem(i).equals(itemsAdapter2.getItem(j))){

               if( itemsAdapter.getItem(i).toString().contains(items2.get(j).toString())){


                for(int k = 0 ; k<is; k++){
                    if ( k == j)

                    images2.add(k,itemsAdapter.image.get(i));
                    //itemsAdapter.image.get(i);
                }
            }



        }
    }
    // lvItems.setAdapter(new CustomAdapter(this, items2,images));
       /* itemsAdapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items2);*/
    itemsAdapter2 = new CustomAdapter(this, items2,images2);
   // itemsAdapter2.notifyDataSetChanged();
    lvItems.setAdapter(itemsAdapter2);

}

CustomAdapter.java

    package com.example.administrator.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;

public class CustomAdapter extends BaseAdapter{
   public static View rowView;
    ArrayList<String> result;
    Context context;
    //Bitmap [] image;
    ArrayList<Bitmap> image;
    private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity, ArrayList<String> items, ArrayList<Bitmap> images) {
        // TODO Auto-generated constructor stub
        result=items;
        context=mainActivity;
        image=images;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();

        rowView = inflater.inflate(R.layout.list_single, null);
        holder.tv=(TextView) rowView.findViewById(R.id.textView1);
        holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
        holder.tv.setText(result.get(position));
       // holder.img.setImageResource(image.get(position));
        holder.img.setImageBitmap(image.get(position));
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+position, Toast.LENGTH_LONG).show();
                Intent intent = new Intent(context.getApplicationContext() ,EditActivity.class);
                String message = Integer.toString(position);
                //long pos = listView.getItemIdAtPosition(position);
               intent.putExtra("message", message);
                context.startActivity(intent);`enter code here`
            }
        });
        return rowView;
    }

}

1 个答案:

答案 0 :(得分:1)

您可以使用Maps而不是两个不同的Arraylists来存储字符串和位图。

您可以将字符串作为键和位图作为值,并迭代地图以获取特定索引的结果。

我希望这能解决你的问题。