안녕하세요.
커스텀 리스트 뷰 예제를 참조해서 샘플을 하나 만들었는데.. 추가버튼을 눌러서 새로운 row를 생성하면
arrayindexoutofboundsexception 에러가 발생합니다.. 구글링을 해보니 없는 index를 참조해서 생기는 오류라는데요,
앱이 강제종료 되고 다시 들어가면 리스트에 새 항목은 추가가 되어있습니다.;
버튼을 눌렀을 때 arrList.add("새항목내용"); 이런식으로 추가해줬는데요, 이 부분을 어댑터 클래스 쪽으로 옮겨야 하는지요?
+조언받고 소스 까지 올립니다..
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
class cAdapter extends BaseAdapter{
private int selectedPos = -1;
private int setPos=0;//
private Context mContext = null;
private ArrayList<String> mListData = new ArrayList<String>();
private boolean[] isCheckedConfrim;
public cAdapter(Context mContext, ArrayList<String> arrayList)
{
super();
this.mContext = mContext;
mListData = arrayList;
this.isCheckedConfrim = new boolean[mListData.size()];
}
public void setChecked(int position) {
isCheckedConfrim[position] = !isCheckedConfrim[position];
}
public ArrayList<Integer> getChecked(){
int tempsize = mListData.size();
ArrayList<Integer> mArrayList = new ArrayList<Integer>();
for(int i = 0; i<tempsize; i++){
if(isCheckedConfrim[i]){
mArrayList.add(i);
}
}
return mArrayList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mListData.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public void setCount(int pos){
pos = mListData.size() ;
// mListData.size() = pos;
// inform the view of this change
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_custom, null);
holder.textview = (TextView) convertView.findViewById(R.id.listText);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_stu);
convertView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
holder.textview.setText(mListData.get(position));
holder.checkbox.setClickable(false);
holder.checkbox.setFocusable(false);
holder.checkbox.setChecked(isCheckedConfrim[position]);
//추가할 때 에러 발생arrayindexoutofboundsexception
//cAdapter.java에서 배열 삭제(poisition)/추가 처리.
// holder.checkbox.setChecked(false);
return convertView;
}
// 리스트뷰에 들어갈 체크박스클래스
public class ViewHolder{
public CheckBox checkbox;
public TextView textview;
}
}
로그 확인해보니,
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 ....
이런식으로 length와 index는 추가할 때마다 증가 합니다.
[물론 추가 버튼을 누르면 앱이 강제종료 되었다가,. 다시 들어가면 또 리스트에 멀쩡히 추가 되어있네요,]
없는 인덱스?를 참조해서 발생하는 에러인가요..?
배열은 0번째부터 시작하니까, 길이가 2면 [0][1] index=0,1 이런식으로 되어야 하는데,
위에 보면 length가 1인데 [0] <-이거하나, index=0이 아닌 index=1을 참조해서 그런건가요..?
아직 이해가 되질 않습니다..ㅠㅠ
항목을 추가하는 부분은 다른 java 클래스에서 추가 버튼을 눌렀을 때
arrList.add(항목텍스트); // 리스트에 추가.
이런식으로 구현했습니다..추가 삭제로 길이가 가변적인 리스트라서 for문으로 일정갯수만큼 딱 추가한다거나 뭐 그런건 없구요..for문 없이 텍스트값 받아서 저렇게 add하도록 햇습니다..ㅠㅠ
로그 찍어보니 arrList.add(); 넘어는 가고, 바로 java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
이런식으로 에러가 찍히네요.add하는 애가 4번째 녀석인데, 왜 length값이 3인지 모르겠습니다. add하면
4로 하나 추가 되고, index는 3인게 맞지 않나요..?
+자꾸 수정하게 되서 죄송합니다;;
log로 확인해봤는데요, 현재 회원이 5명이었을 때, 6명으로 추가하려고 추가버튼을 눌렀는데요,
arrList().add 다음에 나오는 로그가 저 new array list 입니다. 6은 arrList.size() 구요,
다섯명에서 한명 추가했으니 6이 나오는건 맞는거같은데, 그 밑에 바로 에러가 뜨면서 length가 5로 나오네요..
length가 arrList.size와 동일해야 하는거 아닌가요?