公共类TripsMyEventAdapter扩展了RecyclerView.Adapter {
static List<TripData> tripData;
static Context c;
static LocationConverter locationConverter
public TripsMyEventAdapter(List<TripData> tripData, Context c){
this.tripData = tripData;
this.c=c;
locationConverter = new LocationConverter(c);
}
@Override
public TripsMyEventAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
ViewHolder vh;
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyler_bookmark_trips,viewGroup,false);
// create ViewHolder
vh = new ViewHolder(itemLayoutView);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.itemView.setTag(tripData.get(position));
if(tripData.get(position).getImageUrl().contains("https"))
Picasso.with(c).load(tripData.get(position).getImageUrl()).into(viewHolder.image);
else {
Log.d("onBindViewHolder",position+"");
viewHolder.image.setImageBitmap(null);
new LoadImage(viewHolder, position).execute();
}
//loadImage(tripData.get(position).getImageUrl(),viewHolder.image);
viewHolder.title.setText(tripData.get(position).getTitle());
viewHolder.date.setText(tripData.get(position).getDate());
viewHolder.creator.setText(tripData.get(position).getCreator());
viewHolder.description.setText(Html.fromHtml(tripData.get(position).getDescription()).toString().replace("equals","="));
viewHolder.budget.setText(tripData.get(position).getBudget());
if(tripData.get(position).getMaxNum().equals("unlimited"))
viewHolder.partnerNum.setText(tripData.get(position).getMaxNum());
else
viewHolder.partnerNum.setText(tripData.get(position).getMinNum()+"~"+ tripData.get(position).getMaxNum());
viewHolder.eventCreator = tripData.get(position).getCreator();
}
@Override
public int getItemCount() {
return tripData.size();
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title,date,creator,description,budget,partnerNum;
public RelativeLayout container;
public CircleImageView image;
public String eventCreator;
public String firstname,lastname,userName;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
itemLayoutView.setOnClickListener(this);
image = (CircleImageView) itemLayoutView.findViewById(R.id.icon);
title = (TextView) itemLayoutView.findViewById(R.id.tvTitle);
date = (TextView) itemLayoutView.findViewById(R.id.tvDate);
creator = (TextView) itemLayoutView.findViewById(R.id.tvCreator);
description = (TextView) itemLayoutView.findViewById(R.id.tvDescription);
budget = (TextView) itemLayoutView.findViewById(R.id.tvBudget);
partnerNum = (TextView) itemLayoutView.findViewById(R.id.tvNumber);
container = (RelativeLayout)itemLayoutView.findViewById(R.id.container);
}
@Override
public void onClick(View view) {
firstname = LocalDB.getDataBySQL("SELECT firstname FROM User", 0, "firstname");
lastname = LocalDB.getDataBySQL("SELECT lastname FROM User", 0, "lastname");
userName = firstname+" "+lastname;
if(userName.equals(eventCreator)) {
Intent i = new Intent(view.getContext(), CreatorTripDetail.class);
int position = this.getAdapterPosition();
i.putExtra("tid", tripData.get(position).getTid());
i.putExtra("uid", tripData.get(position).getUid());
view.getContext().startActivity(i);
}else {
Intent i = new Intent(view.getContext(),TripDetail.class);
int position = this.getAdapterPosition();
i.putExtra("tid", tripData.get(position).getTid());
i.putExtra("uid", tripData.get(position).getUid());
view.getContext().startActivity(i);
}
}
}
public class LoadImage extends AsyncTask<Object, Void, Void> { //This class use to load the data from server first time
//declare other objects as per your need
Bitmap iconBitmap;
CircleImageView circleImageView;
ViewHolder v;
String url;
int position;
public LoadImage(ViewHolder v, int position){
this.v = v;
circleImageView = v.image;
this.position = position;
url = tripData.get(position).getImageUrl();
}
@Override
protected void onPreExecute()
{
};
@Override
protected Void doInBackground(Object... params)
{
String serverPath = c.getResources().getString(R.string.user)+"getImage";
ImageHelper i = new ImageHelper(c,serverPath,url);
Thread t = new Thread(i);
t.start();
try {
t.join();
iconBitmap = i.getBitmap();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
if(iconBitmap!=null&&circleImageView.getVisibility()==View.VISIBLE&&v.getAdapterPosition()==position) {
circleImageView.setImageBitmap(iconBitmap);
}
};
}
}
和日志消息
03-13 23:38:32.763 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.763 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.793 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.793 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.823 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.823 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.843 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.843 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.853 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.863 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.873 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.873 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.893 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.893 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.903 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.903 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.923 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.923 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.933 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.933 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.943 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.943 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.963 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.963 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.973 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.973 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.983 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:32.983 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:33.003 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:33.003 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:33.013 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2 03-13 23:38:33.013 14586-14586 / org.wings_lab.backpackpairing D / onBindViewHolder:2
重复加载图像30次... 如何解决这个问题?