我正在尝试为listView
创建自定义适配器。这是我的CustomAdapter
课程 -
public class CustomAdapter extends ArrayAdapter<Custom>{
private ArrayList<Custom> entries;
private Activity activity;
private int height;
public CustomAdapter(int hei, Activity a, int textViewResourceId, ArrayList<Custom> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
this.height=hei;
}
public static class ViewHolder{
public TextView item1;
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.review_list_m, null);
v.setMinimumHeight(height);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.name);
holder.image = (ImageView) v.findViewById(R.id.posterView);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Custom custom = entries.get(position);
if (custom != null) {
String imgUrl=custom.getImage();
Log.v("PATH",custom.getcustomBig());
holder.item1.setText(custom.getcustomBig());
}
return v;
}
}
然后我在customclass中只输入了两个值 但以下行登录6次
Log.v("PATH",custom.getcustomBig());
如下所示
01-09 11:49:34.683: V/PATH(12629): Name 1
01-09 11:49:34.683: V/PATH(12629): Name 2
01-09 11:49:34.683: V/PATH(12629): Name 1
01-09 11:49:34.683: V/PATH(12629): Name 2
01-09 11:49:34.683: V/PATH(12629): Name 1
01-09 11:49:34.683: V/PATH(12629): Name 2
但是在listView中只显示了2个条目。
有什么奇怪的吗?因为我需要在listView
上实现下载图像脚本。在那种情况下,该图像会被下载超过1次吗?
答案 0 :(得分:0)
尝试以下代码:(不要将custom
设为最终版,不要检查null
)
Custom custom = entries.get(position);
String imgUrl=custom.getImage();
Log.v("PATH",custom.getcustomBig());
holder.item1.setText(custom.getcustomBig());
答案 1 :(得分:0)
请查看您传递给构造函数的条目列表。将为条目列表中的项目数调用getView()。您的条目列表可能包含6个值。