滚动时getView显示随机项目

滚动时getView显示随机项目

本文介绍了滚动时getView显示随机项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展BaseAdapter的类PlannerListAdapter,我的getView()方法有2个问题.

I have my class PlannerListAdapter that extends BaseAdapter and i have 2 problems in my getView() method.

1)当我将适配器设置为列表视图时,一切正常,列表视图向我显示列表中具有的所有元素,向下滚动,便可以看到列表中的所有元素.但是当我向上滚动时,元素开始相互切换,并且还在重复,这也改变了我元素的顺序,为什么会这样?

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)在我的BrandPlannersList中,我有一个String [] socialNetworks对象.如果社交网络存在于socialNetworks中,我想更改社交网络(Twitter,Facebook,Instagram)的默认图像.我认为我做得对,但是不需要的话,它改变了我在某些行中的某些图像.这是我的getView方法.

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;
}

这是我的viewHolder类

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.抱歉,我的英语水平不是最好.

P.D. I apologize, my English is not the best.

推荐答案

问题已解决,我在适配器中添加了这两种方法:

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;
}

这篇关于滚动时getView显示随机项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:11