列表视图仅显示数组列表的最后一项
我有一个问题。目前,我正在使用 Lazyloading 使用 arraylist 将图像和数据加载到我的列表视图中。但是它只显示数组列表的最后一项。
下面是 LazyLoading 适配器类:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Hashtable<String,String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<Hashtable<String,String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView title;
public TextView author;
public TextView format;
public TextView call_number;
public ImageView cover_image;
public Button next;
public Button previous;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null){
vi = inflater.inflate(R.layout.search_result_display_list , null);
holder=new ViewHolder();
holder.next = (Button) vi.findViewById(R.id.next);
holder.previous = (Button) vi.findViewById(R.id.previous);
holder.title = (TextView) vi.findViewById(R.id.item_title);
holder.author = (TextView) vi.findViewById(R.id.item_author);
holder.format = (TextView) vi.findViewById(R.id.item_format);
holder.call_number = (TextView) vi.findViewById(R.id.item_call_number);
holder.cover_image=(ImageView) vi.findViewById(R.id.cover_image);
vi.setTag(holder);
}
else {
holder=(ViewHolder)vi.getTag();
vi = convertView;
}
for(int i = 0; i < getCount(); i++) {
holder.title.setText(data.get(i).get("title"));
holder.author.setText(data.get(i).get("author"));
holder.format.setText(data.get(i).get("format"));
holder.call_number.setText(data.get(i).get("callNumber"));
holder.cover_image.setTag(data.get(i).get("coverImage"));
imageLoader.DisplayImage(data.get(i).get("coverImage"), activity, holder.cover_image);
}
return vi;
}
我认为问题实际上可能出在我的 for 循环上。它被无限地产生。但我不知道我在输入这段代码时哪里出错了。请帮忙。
I have a problem. Currently i'm using Lazyloading to load images and data into my listview using arraylist. However it only display the last item of the arraylist.
Below is the LazyLoading adapter class:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Hashtable<String,String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<Hashtable<String,String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView title;
public TextView author;
public TextView format;
public TextView call_number;
public ImageView cover_image;
public Button next;
public Button previous;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null){
vi = inflater.inflate(R.layout.search_result_display_list , null);
holder=new ViewHolder();
holder.next = (Button) vi.findViewById(R.id.next);
holder.previous = (Button) vi.findViewById(R.id.previous);
holder.title = (TextView) vi.findViewById(R.id.item_title);
holder.author = (TextView) vi.findViewById(R.id.item_author);
holder.format = (TextView) vi.findViewById(R.id.item_format);
holder.call_number = (TextView) vi.findViewById(R.id.item_call_number);
holder.cover_image=(ImageView) vi.findViewById(R.id.cover_image);
vi.setTag(holder);
}
else {
holder=(ViewHolder)vi.getTag();
vi = convertView;
}
for(int i = 0; i < getCount(); i++) {
holder.title.setText(data.get(i).get("title"));
holder.author.setText(data.get(i).get("author"));
holder.format.setText(data.get(i).get("format"));
holder.call_number.setText(data.get(i).get("callNumber"));
holder.cover_image.setTag(data.get(i).get("coverImage"));
imageLoader.DisplayImage(data.get(i).get("coverImage"), activity, holder.cover_image);
}
return vi;
}
I'm thinking that the problem might actually be with my for-loop. Where it is being generated infinitely. But I don't know where did I go wrong in typing this code. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除 for 循环并使用
position
代替data.get(i)
中的 iRemove the for loop and in place of i in
data.get(i)
useposition