마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

gridview 에러 질문 드립니다

0 추천

<이미지 변수배열>

private Integer[] gridviewimage = {R.drawable.food_01 ,R.drawable.food_02,R.drawable.food_03,R.drawable.food_04,R.drawable.food_05
            , R.drawable.food_06,R.drawable.food_07};

 

<gridview Adapter소스>

public class gridAdapter extends BaseAdapter
        {
            LayoutInflater inflater2;    
            public gridAdapter()
            {
                inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            

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

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

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

            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                if(convertView == null)
                {
                    convertView = inflater2.inflate(R.layout.image_adapter, parent,false);
                }
                
                final ResolveInfo info = apps.get(position);
                ImageView imageView = (ImageView) convertView.findViewById(R.id.iv001);
                
                if(position <= gridviewimage.length)
                {
                    imageView.setImageResource(gridviewimage[position]);
                }
                imageView.setOnClickListener(new Button.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                    // TODO Auto-generated method stub
                        
                    }
                            });
                         return convertView;
                
            }
            
        }

 

밑줄 친 부분대로 실행을 하면 로그창에 오류내용이

java.lang.ArrayIndexOutOfBoundsException: length=7; index=7

이런식으로 나옵니다.

position으로 들어오는값이 배열의 길이보다 커서 그런거같은데 어떤식으로 해결을 해야하나요?도움 부탁드립니다

pooheoul (600 포인트) 님이 2015년 9월 30일 질문
pooheoul님이 2015년 9월 30일 수정

1개의 답변

+1 추천
 
채택된 답변

겟 카운트의 app.size() 부분을 gridviewimage .length()로 변경하세요

익명사용자 님이 2015년 10월 1일 답변
pooheoul님이 2015년 10월 12일 채택됨
...