列表视图项目出现在错误的位置或正在重复

时间:2015-09-04 10:41:55

标签: android android-listview imageview

HI首先是的,这个问题已经被问到,但我已经检查过并尝试了所有答案,但没有任何效果。

我不知道这是怎么回事。但是我没有在Listview中获得正确的图像。

这是我的Adapter类

import java.util.List;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.RadioButton;
import android.widget.TextView;

public class AdapterForNewsFeed extends BaseAdapter {

    Context context;
    List<News> rowCate;
    LayoutInflater mInflater;

    AdapterForNewsFeed(Context context, List<News> rowCate) {
        this.context = context;
        this.rowCate = rowCate;
        // NewsFeeds.newsfeedlist.setOnScrollListener(new SampleScrollListener(
        // context));

    }

    @Override
    public int getCount() {

        return rowCate.size();
    }

    @Override
    public Object getItem(int position) {

        return rowCate.get(position);
    }

    @Override
    public long getItemId(int position) {

        return rowCate.indexOf(getItem(position));
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder1 hold = null;

        if (convertView == null) {
            mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            hold = new viewHolder1();
            convertView = mInflater.inflate(R.layout.feeds_list_items, null);

            convertView.setTag(hold);
        }

        else {

            hold = (viewHolder1) convertView.getTag();

            hold.feedation = (ImageView) convertView
                    .findViewById(R.id.feedactionimg);

            hold.name = (TextView) convertView.findViewById(R.id.fullnametxt);
            hold.date = (TextView) convertView.findViewById(R.id.datetxt);
            hold.description = (TextView) convertView
                    .findViewById(R.id.descriptiontxt);
            hold.likes = (TextView) convertView.findViewById(R.id.likestxt);
            hold.dislikes = (TextView) convertView
                    .findViewById(R.id.dislikestxt);
            hold.comments = (TextView) convertView
                    .findViewById(R.id.commentstxt);
            hold.share = (TextView) convertView.findViewById(R.id.sharetxt);
            hold.post = (ImageView) convertView.findViewById(R.id.postimg);

            final News row_pos = rowCate.get(position);

            hold.name.setText(row_pos.getfullname());
            hold.date.setText(row_pos.getposted_date());
            hold.description.setText(row_pos.getdescription());
            hold.likes.setText(row_pos.getlikeCount());
            hold.dislikes.setText(row_pos.getdislikeCount());
            hold.comments.setText(row_pos.getcommentCount());
            hold.share.setText(row_pos.getshareCount());

            //
            // if
            // (!row_pos.getphoto().toString().trim().isEmpty()) {
            // i.DisplayImage("http://www.risalati.com/myhubber/uploads/posts/"
            // + row_pos.getphoto(), post);
            // }
            //
            hold.feedation.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    onPopupButtonClick(v);

                }
            });

            if (!row_pos.getphoto().toString().trim().isEmpty()) {

                UrlImageViewHelper.setUrlDrawable(hold.post,
                        "http://www.risalati.com/myhubber/uploads/posts/"
                                + row_pos.getphoto());

            }
        }
        return convertView;

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    public void onPopupButtonClick(View v) {

        PopupMenu popUp = new PopupMenu(context, v);
        popUp.getMenuInflater().inflate(R.menu.popup, popUp.getMenu());

        popUp.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {

                return true;
            }
        });

        popUp.show();

    }

    class viewHolder1 {
        ImageView feedation;
        TextView name;
        TextView date;
        TextView description;
        TextView likes;
        TextView dislikes;
        TextView comments;
        TextView share;
        ImageView post;

    }

}

我也尝试了Picasso,ImageLoader Class,LazyLoader,但没有任何工作。

请帮忙。 感谢

3 个答案:

答案 0 :(得分:2)

您尝试实施ViewHolder模式很好。请查看RecyclerView参考。

问题是你没有以正确的方式实现模式。如果视图为null,则必须对其进行膨胀并获取引用。如果它不为null,则获取该对象。无论如何,最后你填写数据。

检查this link

答案 1 :(得分:1)

用以下代码替换您的代码。可能会帮助您解决问题。

public class AdapterForNewsFeed extends BaseAdapter {

    Context context;
    List<News> rowCate;
    LayoutInflater mInflater;
    viewHolder1 hold;

    AdapterForNewsFeed(Context context, List<News> rowCate) {
        this.context = context;
        this.rowCate = rowCate;
        mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {

        return rowCate.size();
    }

    @Override
    public Object getItem(int position) {

        return rowCate.get(position);
    }

    @Override
    public long getItemId(int position) {

        return rowCate.indexOf(getItem(position));
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        hold = new viewHolder1();

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.feeds_list_items, null);

        hold.feedation = (ImageView) convertView.findViewById(R.id.feedactionimg);
            hold.name = (TextView) convertView.findViewById(R.id.fullnametxt);
            hold.date = (TextView) convertView.findViewById(R.id.datetxt);
            hold.description = (TextView) convertView
                    .findViewById(R.id.descriptiontxt);
            hold.likes = (TextView) convertView.findViewById(R.id.likestxt);
            hold.dislikes = (TextView) convertView
                    .findViewById(R.id.dislikestxt);
            hold.comments = (TextView) convertView
                    .findViewById(R.id.commentstxt);
            hold.share = (TextView) convertView.findViewById(R.id.sharetxt);
            hold.post = (ImageView) convertView.findViewById(R.id.postimg);

            convertView.setTag(hold);
        }

        else {      hold = (viewHolder1) convertView.getTag();  }



            final News row_pos = rowCate.get(position);

            hold.name.setText(row_pos.getfullname());
            hold.date.setText(row_pos.getposted_date());
            hold.description.setText(row_pos.getdescription());
            hold.likes.setText(row_pos.getlikeCount());
            hold.dislikes.setText(row_pos.getdislikeCount());
            hold.comments.setText(row_pos.getcommentCount());
            hold.share.setText(row_pos.getshareCount());


            hold.feedation.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    onPopupButtonClick(v);

                }
            });

            if (!row_pos.getphoto().toString().trim().isEmpty()) {

                UrlImageViewHelper.setUrlDrawable(hold.post,
                        "http://www.risalati.com/myhubber/uploads/posts/"
                                + row_pos.getphoto());

            } else {
            // Provide the Default Image
        }   
        }
        return convertView;

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    public void onPopupButtonClick(View v) {

        PopupMenu popUp = new PopupMenu(context, v);
        popUp.getMenuInflater().inflate(R.menu.popup, popUp.getMenu());

        popUp.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {

                return true;
            }
        });

        popUp.show();

    }

    class viewHolder1 {
        ImageView feedation;
        TextView name;
        TextView date;
        TextView description;
        TextView likes;
        TextView dislikes;
        TextView comments;
        TextView share;
        ImageView post;

    } 
}

答案 2 :(得分:0)

所以我真的不知道为什么会这样,只是替换这个

     if (!row_pos.getphoto().toString().trim().isEmpty()) {

            UrlImageViewHelper.setUrlDrawable(hold.post,
                    "http://www.risalati.com/myhubber/uploads/posts/"
                            + row_pos.getphoto());

        }

只有

UrlImageViewHelper.setUrlDrawable(hold.post,
                    "http://www.risalati.com/myhubber/uploads/posts/"
                            + row_pos.getphoto());

解决了这个问题。只需更换if条件即可。

感谢每一位人士的帮助。