问题
I have my class PlannerListAdapter that extends BaseAdapter and i have 2 problems in my getView() method.
1) When i set my adapter to the listview, everything it's ok, the listview show me all the elements i have in my List, I scroll down and i can see all the elements of the list. But when i scroll up, the elements starts to switch each other and also are being repeated, also, it is changing me the order of the elements, why is this happening?
2) In my BrandPlannersList i have a String[] socialNetworks object. I want to change the social networks (twitter, facebook, instagram) default image if the social network exist in socialNetworks. I think i am doing it right but it's changing me some images in some rows when it doesn't have to. Here's my getView method.
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.planificacion_item, null);
viewHolder = new ViewHolder();
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.date);
viewHolder.txtTime = (TextView) convertView.findViewById(R.id.time);
viewHolder.txtContent = (TextView) convertView.findViewById(R.id.content);
viewHolder.txtStatus = (TextView) convertView.findViewById(R.id.status);
viewHolder.txtBrandName = (TextView) convertView.findViewById(R.id.brandNameTextView);
viewHolder.imgInstagram = (ImageView) convertView.findViewById(R.id.instagramPlanImg);
viewHolder.imgTwitter = (ImageView) convertView.findViewById(R.id.twitterPlanImg);
viewHolder.imgFacebook = (ImageView) convertView.findViewById(R.id.facebookPlanImg);
viewHolder.contentImg = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)item.getTag();
}
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.txtDate.setText(brandPlannersList.get(position).publicationDate);
viewHolder.txtTime.setText(brandPlannersList.get(position).publicationTime);
viewHolder.txtBrandName.setText(brandPlannersList.get(position).brandName);
if (brandPlannersList.get(position).status.equalsIgnoreCase("0")) {
viewHolder.txtStatus.setText(R.string.to_publish);
} else {
viewHolder.txtStatus.setText(R.string.published);
}
viewHolder.txtContent.setText(brandPlannersList.get(position).content);
if (brandPlannersList.get(position).image.trim().equalsIgnoreCase("")) {
viewHolder.contentImg.setVisibility(View.GONE);
} else {
Log.d("Imagen", brandPlannersList.get(position).image + "");
new ImageLoadTask(context.getResources().getString(R.string.short_endpoint) + brandPlannersList.get(position).image, viewHolder.contentImg).execute();
}
//This is where i'm trying to change the social networks image
if (brandPlannersList.get(position).socialNetworks.length > 0) {
for (int i = 0; i < brandPlannersList.get(position).socialNetworks.length; i++) {
Log.d("PlannerListAdapter", "Array de redes sociales en planner en posicion: "+position +" "+ Arrays.toString(brandPlannersList.get(position).getSocialNetworks()));
if (brandPlannersList.get(position).socialNetworks[i].equalsIgnoreCase("twitter")) {
viewHolder.imgTwitter.setImageDrawable(twDrawable);
}
if (brandPlannersList.get(position).socialNetworks[i].equalsIgnoreCase("instagram")) {
viewHolder.imgInstagram.setImageDrawable(inDrawable);
}
if (brandPlannersList.get(position).socialNetworks[i].equalsIgnoreCase("facebook")) {
viewHolder.imgFacebook.setImageDrawable(fbDrawable);
}
}
}
return convertView;
}
and this is my viewHolder class
static class ViewHolder{
TextView txtDate;
TextView txtTime;
TextView txtContent;
TextView txtStatus;
TextView txtBrandName;
ImageView imgInstagram;
ImageView imgTwitter;
ImageView imgFacebook;
ImageView contentImg;
}
the rest of the methods in case you want to see it:
public PlannerListAdapter(List<BrandPlanners> brandPlannersList, Drawable twDrawable, Drawable inDrawable, Drawable fbDrawable, Context context) {
super();
this.brandPlannersList = brandPlannersList;
this.twDrawable = twDrawable;
this.inDrawable = inDrawable;
this.fbDrawable = fbDrawable;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return brandPlannersList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
P.D. I apologize, my English is not the best.
回答1:
Problem resolved, i have added this two methods in my adapter:
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
来源:https://stackoverflow.com/questions/32231874/getview-showing-random-items-when-scrolling