안녕하세요.
프로젝트 진행중에 customAdapter를 이용해서 ListView에 이미지와 텍스트를 출력하려고 하는데요.
일단 api를 사용해서 이미지 url과 텍스트를 가져오는데까지는 성공을 햇는데 ListView한줄에 두개의 아이템(즉 두개의 dto)이 들어가야되는데 반환되는 ArrayList는 하나입니다.
때문에 한 listview에 두개의 dto를 어떻게 쪼개넣는게 좋을까요?
설명이 조금 이상한것 같은데 예제를 보여드리자면
public class NewBookAdapter extends BaseAdapter {
private ArrayList<NewBookDto> list;
private NewBookDto lDto;
private NewBookDto rDto;
private Activity activity;
private static LayoutInflater inflater;
private ImageLoader imageLoader;
public NewBookAdapter(Activity activity, ArrayList list) {
this.list = list;
this.activity = activity;
inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(this.activity.getApplicationContext());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
int i = position + 1;
try {
if(i % 2 == 0) {
if(list.get(i) != null)
lDto = list.get(i);
} else {
if(list.get(i) != null)
rDto = list.get(i);
}
} catch(IndexOutOfBoundsException iobe) {
lDto = null;
rDto = null;
iobe.printStackTrace();
}
if(view == null) {
view = inflater.inflate(R.layout.item_newbooklist, null);
}
if(lDto != null) {
TextView txtNewBookTitleL = (TextView)view.findViewById(R.id.txtNewBookTitleL);
TextView txtNewBookTitleR = (TextView)view.findViewById(R.id.txtNewBookTitleR);
txtNewBookTitleL.setText(lDto.getTitle());
if(rDto != null)
txtNewBookTitleR.setText(rDto.getTitle());
ImageView imgNewBookL = (ImageView)view.findViewById(R.id.imgNewBookL);
ImageView imgNewBookR = (ImageView)view.findViewById(R.id.imgNewBookR);
if(lDto.getCoverSmallUrl() != null)
imageLoader.DisplayImage(lDto.getCoverSmallUrl(), imgNewBookL);
if(rDto.getCoverSmallUrl() != null)
imageLoader.DisplayImage(rDto.getCoverSmallUrl(), imgNewBookR);
imgNewBookL.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.d("test", lDto.getTitle());
}
});
imgNewBookR.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.d("test", rDto.getTitle());
}
});
}
return view;
}
}
이런식으로 두개의 dto가 들어가고 두개의 dto가 따로 나와야되는데 이미지따로 텍스트따로 노내요 아 텍스트로 설명하려고하니까 설명이 더 어려운것 같은데 혹시나 제 질문이 이해가시면 답변좀 달아주세요~
일단 저는 position값이 짝수냐 홀수로 나눠놨는데 이것 도 안되는 거라서 일단 올리긴 햇습니다.감사합니다.