我在android中有一个自定义ArrayAdapter用于填充我的自定义ListView。但是,只有最后一项显示在我的ListView上,并从第一项迭代到最后一项(我的数组中的最后一项在listview中显示x次,其中x是数组中的项数)。我试图寻找答案,但这些解决了视图的回收问题,这使得listview重复了前几个项目。然而,我从头到尾重复最后一项。我尝试了那里的建议,但我的代码仍然运行相同。数组内容很好,因为在日志中打印正确的数组内容。感谢任何帮助。这是我的代码
public class VideoQueueAdapter extends BaseAdapter implements View.OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
VideoListModel vid_list_item=null;
int i=0;
private Context context;
/************* CustomAdapter Constructor *****************/
public VideoQueueAdapter(Activity a, ArrayList d,Resources resLocal) {
this.context = context;
/********** Take passed val0ues **********/
activity = a;
data=d;
res = resLocal;
context = activity.getApplicationContext();
/*********** Layout inflator to call external xml layout () **********************/
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder to contain inflated xml file elements ***********/
public static class ViewHolder{
public TextView title;
public TextView url;
public TextView deadline;
public TextView id;
public ImageView thumbnail;
}
/*********** Depends upon data size called for each row , Create each ListView row ***********/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(vi==null){
/********** Inflate tabitem.xml file for each row ( Defined below ) ************/
vi = inflater.inflate(R.layout.activity_video_item, null);
/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
}
else{ holder=(ViewHolder)vi.getTag();}
holder.thumbnail = (ImageView)vi.findViewById(R.id.vid_thumb);
holder.title = (TextView)vi.findViewById(R.id.vid_title);
holder.url = (TextView)vi.findViewById(R.id.vid_url);
holder.deadline = (TextView)vi.findViewById(R.id.item_timeout);
holder.id = (TextView)vi.findViewById(R.id.item_id);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
if(data.size()<=0)
{
holder.title.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
vid_list_item=null;
vid_list_item = (VideoListModel) data.get(position);
/************ Set Model values in Holder elements ***********/
String title = "";
String img = vid_list_item.getUrl();
img = "http://img.youtube.com/vi/"+ img+ "/0.jpg";
Picasso.with(context).load(img).resize(115, 115).into(holder.thumbnail);
holder.title.setText(vid_list_item.getTitle());
holder.url.setText(vid_list_item.getUrl());
holder.deadline.setText(vid_list_item.getTimeout());
holder.id.setText(vid_list_item.getId());
/******** Set Item Click Listner for LayoutInflater for each row ***********/
vi.setOnClickListener(new OnItemClickListener(position));
}
return vi;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked");
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements View.OnClickListener {
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
// CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
// sct.onItemClick(mPosition);
}
}
}
答案 0 :(得分:0)
我认为使用viewholder的正确方法是这样的
if(vi==null){
/********** Inflate tabitem.xml file for each row ( Defined below ) ************/
vi = inflater.inflate(R.layout.activity_video_item, null);
/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
holder.thumbnail = (ImageView)vi.findViewById(R.id.vid_thumb);
holder.title = (TextView)vi.findViewById(R.id.vid_title);
holder.url = (TextView)vi.findViewById(R.id.vid_url);
holder.deadline = (TextView)vi.findViewById(R.id.item_timeout);
holder.id = (TextView)vi.findViewById(R.id.item_id);
}
else{ holder=(ViewHolder)vi.getTag();}
试试这个,也许它可以解决你的问题
答案 1 :(得分:0)
除上述答案外,还要确保您已经发现
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
适配器中的。 :)