M
M
MCjores2019-06-16 19:38:58
Android
MCjores, 2019-06-16 19:38:58

Why is an OutOfBoundException thrown when the ListView is filled?

Good day!
When filling the ListView, an error occurs, out of bounds of the array, although I add only 1 object.
The code

public class RestaurantMenuFragment extends Fragment {

    private int DishPhoto;
    private String DishName;
    private int Weight;
    private int Price;
    private ArrayList<DishMenu> menus;
    private Context context;
    private ListView listView;


    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.restaurant_menu, container, false);
        listView = view.findViewById(R.id.restaurant_menu_list);
        context = inflater.getContext();
        menus = new ArrayList<>();
        menus.add(new DishMenu("Яичница с Авокадо","Питательный и очень полезный завтрак для хорошего настроения",
                new String[]{"Яйца","авокадо","помидоры","мука пшеничная", "молоко", "лук","соль","черный перец","оливковое масло","укроп"},
                151,33,4.7,277,250,350));
        RestaurantMenuAdapter adapter = new RestaurantMenuAdapter(context, menus);
        
        listView.setAdapter(adapter);
        Log.i("RestMenuFragment", "return view");
        return view;

    }

    }


    private class RestaurantMenuAdapter extends ArrayAdapter<DishMenu> {

        private Context context;

        public RestaurantMenuAdapter(Context context, ArrayList<DishMenu> menu) {
            super(context, R.layout.restaurant_menu_item, menu);
            this.context = context;
        }


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


        @Override
        public DishMenu getItem(int position) {
            return menus.get(position);
        }

        

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.i("getView", "position " + position);
            DishMenu menu = getItem(position);

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.restaurant_menu_item, parent, false);

            ImageView photoDish = view.findViewById(R.id.iv_menu_photo_dish);
            TextView nameDish = view.findViewById(R.id.tv_menu_name);
            TextView weightDish = view.findViewById(R.id.tv_menu_weight);
            Button addToBuyList = view.findViewById(R.id.btn_menu_add);

            if (menu != null) {
                
//                photoDish.setImageResource(menu.get);
                nameDish.setText(menu.getName());
                weightDish.setText(String.valueOf(menu.getWeight()));
                addToBuyList.append(String.valueOf(menu.getPrice()));
            }
           
            return view;
        }
    }
}

Addition
In another fragment with exactly the same structure, everything works.
Maybe this is important - this fragment is nested.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
First Last, 2017-08-02
@DeniSidorenko

> $("#text_comment").text(str);
$("#text_comment").appendTo(str);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question