滚动时getView显示随机项

时间:2015-08-26 16:24:26

标签: android listview android-listview baseadapter getview

我的类PlannerListAdapter扩展了BaseAdapter,我的getView()方法遇到了2个问题。

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

2)在我的BrandPlannersList中,我有一个String [] socialNetworks对象。如果社交网络存在于socialNetworks中,我想更改社交网络(twitter,facebook,instagram)默认图像。我认为我做得对,但是当它不必要的时候,它会在某些行中改变我的一些图像。这是我的getView方法。

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类

static class ViewHolder{
    TextView txtDate;
    TextView txtTime;
    TextView txtContent;
    TextView txtStatus;
    TextView txtBrandName;
    ImageView imgInstagram;
    ImageView imgTwitter;
    ImageView imgFacebook;
    ImageView contentImg;
}

其他方法,以防你想看到它:

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

Pd积。我道歉,我的英语不是最好的。

1 个答案:

答案 0 :(得分:1)

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

@Override
public int getViewTypeCount() {                 
return getCount();
}

@Override
public int getItemViewType(int position) {
return position;
}