Search code examples
androidindexoutofboundsexceptioncustom-adapterhorizontallist

IndexOutOfBoundsException: Invalid index 1, size is 0


I want to display items selected from listview1(vertical) to listview2(horizontal). When I add items to my list i get error :

12668-12668/com.example.vdovin.restaurantmenu E/AndroidRuntime﹕ FATAL EXCEPTION: main   java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.vdovin.restaurantmenu.CustomBaseAdapter.getItem(CustomBaseAdapter.java:56)
        at com.example.vdovin.restaurantmenu.MenuActivity$1.onItemClick(MenuActivity.java:230)
        at android.widget.AdapterView.performItemClick(AdapterView.java:298)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1130)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2818)
        at android.widget.AbsListView$1.run(AbsListView.java:3498)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5137)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
        at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
        at dalvik.system.NativeStart.main(Native Method)

I do not understand why size of the List does not expand with the addition of elements in it. It work only if I add all items in list and then display listview

From MenuActivity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.variety_of_dishes);
    listView2 = (ListView)findViewById(R.id.listView2);
    horizontalListView = (HorizontalListView)findViewById(R.id.horizontalListView);
    listObjects = new ArrayList<MenuItem>();
    getDishesList();

    ownAdapter = new CustomBaseAdapter(listObjects, MenuActivity.this);
    horizontalListView.setAdapter(ownAdapter);

    listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            testObject = ownAdapter.getItem(position);
            final Object test2Object = ownAdapter.priceOfDishes(position);

            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MenuActivity.this);

            alertDialogBuilder.setTitle("Заказ");
            alertDialogBuilder.setMessage("Сколько порций вы хотите заказать?");
            final EditText numberOfDishes = new EditText(MenuActivity.this);
            numberOfDishes.setInputType(InputType.TYPE_CLASS_NUMBER);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            numberOfDishes.setLayoutParams(lp);
            alertDialogBuilder.setView(numberOfDishes);

            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setPositiveButton("Подтвердить", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    addToOrderList((MenuItem) testObject);

                }
            });
            alertDialogBuilder.setNegativeButton("Отмена", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    });

}

public void addToOrderList(MenuItem object){

        listObjects.add(object);
    ownAdapter.notifyDataSetChanged();


}

CustomBaseAdapter:

public class CustomBaseAdapter extends BaseAdapter {
private static LayoutInflater inflater=null;


Context context;
List<MenuItem> menuItems;


public CustomBaseAdapter(List<MenuItem> menuItems, Context context) {
    this.menuItems = menuItems;
    this.context = context;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

private class ViewHolder{
    ImageView imageView;
    TextView txtTitle;
    TextView txtPrice;
}

public double priceOfDishes(int position){

    MenuItem doubleItem = (MenuItem)getItem(position);
    String priceItem = doubleItem.getItemPrice();
    double finalPrice = Double.parseDouble(priceItem);

    return finalPrice;
}

@Override
public int getCount() {
    return menuItems.size();
}

@Override
public Object getItem(int position) {
    return menuItems.get(position);
}

@Override
public long getItemId(int position) {
    return menuItems.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;

    LayoutInflater myInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if(convertView ==null || !(convertView.getTag() instanceof ViewHolder) ){
        convertView = myInflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.imageView = (ImageView)convertView.findViewById(R.id.image_View);
        viewHolder.txtTitle = (TextView)convertView.findViewById(R.id.item_title);
        viewHolder.txtPrice = (TextView) convertView.findViewById(R.id.price);
        convertView.setTag(myInflater);
    } else {
       viewHolder = (ViewHolder) convertView.getTag();
    }
    MenuItem menuItem = (MenuItem)getItem(position);

    viewHolder.imageView.setImageResource(menuItem.getImageId());
    viewHolder.txtTitle.setText(menuItem.getItemTitle());
    viewHolder.txtPrice.setText(menuItem.getItemPrice());

    return convertView;
}}

Solution

  • Your listView2 doesn't seem to be connected to the adapter. So a click on the first item of listView2 will be treated by your ItemClickListener, which works with the adapter of the horizontalListView.

    The problem is just here :

    testObject = ownAdapter.getItem(position);
    

    You are adding items in the very same list you are reading. I think you are missing another list or another adapter...