티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView
리스트뷰에서 ArrayIndexOutOfBoundsException 오류 수정하기
문제 내용
I am attempting to create a custom Adapter for my ListView since each item in the list can have a different view (a link, toggle, or radio group), but when I try to run the Activity that uses the ListView I receive an error and the app stops. The application is targeted for the Android 1.6 platform.
저는 ListView에 다른 뷰(링크, 토글 또는 라디오 그룹)를 가진 항목이 포함될 수 있기 때문에 커스텀 어댑터를 만들려고 노력 중입니다. 그러나 ListView를 사용하는 Activity를 실행하려고하면 오류가 발생하고 앱이 중지됩니다. 이 어플리케이션은 Android 1.6 플랫폼을 대상으로 합니다.
The code:
코드:
public class MenuListAdapter extends BaseAdapter {
private static final String LOG_KEY = MenuListAdapter.class.getSimpleName();
protected List<MenuItem> list;
protected Context ctx;
protected LayoutInflater inflater;
public MenuListAdapter(Context context, List<MenuItem> objects) {
this.list = objects;
this.ctx = context;
this.inflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(LOG_KEY, "Position: " + position + "; convertView = " + convertView + "; parent=" + parent);
MenuItem item = list.get(position);
Log.i(LOG_KEY, "Item=" + item );
if (convertView == null) {
convertView = this.inflater.inflate(item.getLayout(), null);
}
return convertView;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public MenuItem getItem(int position) {
return this.list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
Log.i(LOG_KEY, "getItemViewType: " + this.list.get(position).getLayout());
return this.list.get(position).getLayout();
}
@Override
public int getViewTypeCount() {
Log.i(LOG_KEY, "getViewTypeCount: " + this.list.size());
return this.list.size();
}
}
The error I receive:
에러 메시지:
java.lang.ArrayIndexOutOfBoundsException
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3523)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1158)
at android.widget.ListView.onMeasure(ListView.java:1060)
at android.view.View.measure(View.java:7703)
I do know that the application is returning from getView
and everything seems in order.
getView에서 반환하는 것이 모두 정상적으로 보인다는 것은 알고 있습니다.
Any ideas on what could be causing this would be appreciated.
이 문제를 일으키는 원인에 대한 어떠한 아이디어도 있다면 감사하겠습니다.
Thanks,
감사합니다.
-Dan
높은 점수를 받은 Solution
The item view type you are returning from
getItemViewType()
is >= getViewTypeCount()
.
getItemViewType()에서 반환하는 아이템 뷰 타입이 getViewTypeCount() 이상입니다.
가장 최근 달린 Solution
The accepted answer is correct. This is what I am doing to avoid the problem:
채택된 답변이 정확합니다. 이것은 문제를 피하기 위해 내가 하는 것입니다:
public enum FoodRowType {
ONLY_ELEM,
FIRST_ELEM,
MID_ELEM,
LAST_ELEM
}
@Override
public int getViewTypeCount() {
return FoodRowType.values().length;
}
@Override
public int getItemViewType(int position) {
return rows.get(position).getViewType(); //returns one of the above types
}
출처 : https://stackoverflow.com/questions/2596547/arrayindexoutofboundsexception-with-custom-android-adapter-for-multiple-views-in
'개발 > 안드로이드' 카테고리의 다른 글
텍스트를 공유하는 모든 앱에 인텐트를 통해 텍스트 공유하기 (0) | 2023.02.08 |
---|---|
NotificationCompat.Builder 제대로 사용하기 (0) | 2023.02.08 |
클래스를 상속하였을 때 인플레이트 오류 수정하기 (0) | 2023.02.07 |
Environment.getExternalStorageDirectory() 대신 내부 저장소 디렉토리 가져오기 (0) | 2023.02.07 |
부모 액티비티로 돌아가기 (0) | 2023.02.06 |