안드로이드 개발 질문/답변
(글 수 45,052)
우선 BaseAdapter 개념으로 작업을 해봤는데 안더더군요 ㅠ,ㅠ
아래 기본으로 한 작업은 내용이 잘 출력됩니다..
(cursor는 DBUtil로 잘받아왔습니다)
SimpleCursorAdapter Adapter = null;
Adapter = new SimpleCursorAdapter( this,
R.layout.custom_list,
cursor,
new String[] {"txt","base_txt"},
new int[] { R.id.custom_list_txt,R.id.custom_list_base_txt}
);
그런데 위 에서 layout을 종류에 따라 다르게 보여야되서 SimpleCursorAdapter 를 좀 바꿔보았습니다.
public class CustomList extends SimpleCursorAdapter {
private final String logTag = "HANJA";
private Context context;
private LayoutInflater inflater;
private int layout;
public CustomList(Context context, int layout, Cursor c, String[] from,int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
this.context = context;
this.layout = layout;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder{
TextView txt;
TextView base_txt;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(layout,parent,false);
holder = new ViewHolder();
holder.txt = (TextView) convertView.findViewById(R.id.custom_list_txt);
holder.base_txt = (TextView) convertView.findViewById(R.id.custom_list_base_txt);
}else{
holder = (ViewHolder) convertView.getTag();
}
Cursor cursor = getCursor();
int nameCol = cursor.getColumnIndex("txt");
String name = cursor.getString(nameCol);
Log.d(logTag,"Cursor : "+nameCol+" : "+name);
return convertView;
}
신기하게 위에 nameCol에서는 1을 반환합니다.
그러나 cursor.getString(1)을 하면 에러가 납니다.
layout변환코드는 우선 제대로 작동조차 안되서 제거된상태구요 holder패턴만 적용되있습니다.
CustomList Adapter = null;
Adapter = new CustomList( this,
R.layout.custom_list,
cursor,
new String[] {"txt","base_txt"},
new int[] { R.id.custom_list_txt,R.id.custom_list_base_txt}
);
작업후 위에 처럼 쓴느데 이부분에서도 좀 의문이
벌써 cursor을 구할때 컬럼을 배열을 다 넣었는데
위 String[],int[]배열이 구지 필요한지...
customList에서 getView(int position, View convertView, ViewGroup parent) 에서 layout을 개별 로드해서
cursor와 뿌릴 객체 맵핑작업을 하는데..
아무튼 우선은 기본 동작이 안됩니다.
왜인가요?
E/AndroidRuntime( 5393): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5992
E/AndroidRuntime( 5393): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
E/AndroidRuntime( 5393): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214
)
E/AndroidRuntime( 5393): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
E/AndroidRuntime( 5393): at packageName.CustomList.getView(CustomList.java:49)
E/AndroidRuntime( 5393): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
E/AndroidRuntime( 5393): at android.widget.ListView.makeAndAddView(ListView.java:1727)
E/AndroidRuntime( 5393): at android.widget.ListView.fillDown(ListView.java:652)
E/AndroidRuntime( 5393): at android.widget.ListView.fillFromTop(ListView.java:709)
E/AndroidRuntime( 5393): at android.widget.ListView.layoutChildren(ListView.java:1580)
E/AndroidRuntime( 5393): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
....
2011.02.28 17:17:02
zihen님 답변감사요.
현재 여려거지고 debug를 하고있습니다.
혹시몰라서 다 제거하고 cursor만 찍어봤습니다.
public View getView(int position, View convertView, ViewGroup parent) {
Cursor cursor = getCursor();
try{
Log.d(logTag,"Cursor["+position+"]");
for(int i=0;i<cursor.getColumnCount();i++){
Log.d(logTag," Cursor["+i+"]: "+cursor.getString(i));
}
}catch(Exception e){
Log.d(logTag,"Cursor ERROR: "+position);
}
return super.getView(position, convertView, parent);
}
커서가 이상없이 찍혔습니다.
문제는 커서가 아니라
ViewHolder holder;
if(convertView == null){
Log.d(logTag," Cursor 1---------------------------------------------");
convertView = inflater.inflate(layout,parent,false);
holder = new ViewHolder();
holder.txt = (TextView) convertView.findViewById(R.id.custom_list_txt);
holder.base_txt = (TextView) convertView.findViewById(R.id.custom_list_base_txt);
}else{
Log.d(logTag," Cursor 2---------------------------------------------");
holder = (ViewHolder) convertView.getTag();
}
이렇게 레이아웃을 로드하는 부분이였습니다.
holder 패턴이면 cursor1이 한번만 출력되고 cursor2가 재사용되는 식인데.
죄다 cursor1로그만 찍히네요
public CustomList(Context context, int layout, Cursor c, String[] from,int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
this.context = context;
this.layout = layout;
//inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = LayoutInflater.from(context);
}
생성자에서 위 두가지 방식으로 로드를 해봤지만 역시 허사 입니다.
moveToPosition는 자동호출되는건지 우선 커서 데이터 부분에서는 문제가 없는것으로 보이고요
inflater를 못가져오는 이유를 찾고 있습니다.




bindView내부에서 생성자에 넣는 String[], int[] 를 사용하지요.
getView를 오버라이딩 하셔서 bindView가 호출이 안 될테니
null을 넣으시거나 대충 new String[]{}과 같이 가짜를 넣으셔도 됩니다.
android.database.CursorIndexOutOfBoundsException: Index -1
이거로 봐서는 해당 컬럼이 존재하지 않다고 봐야겠죠.
찬찬이 디버깅해보세요.
한가지....
getView안에서 직접 Cursor를 사용하시니
moveToPosition을 해주셔야....