ViewHolder + ListView滚动随机文本

时间:2014-02-10 04:33:53

标签: android listview android-adapter

我知道在StackOverflow上反复询问过这个问题,但是我经历过一堆问题/答案,并且无法弄清楚我的代码存在什么问题。这是我的自定义适配器中的getView代码:

@Override public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.friend_list_item, parent, false);
        holder = new ViewHolder();
        holder.image = (ImageView) view.findViewById(R.id.friend_pic);
        holder.text = (TextView) view.findViewById(R.id.friend_name);
        holder.num_classes = (TextView) view.findViewById(R.id.shared_classes);         
        holder.text.setTypeface(face);
        holder.num_classes.setTypeface(lightface);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    // Build Shared Classes text
    FriendDataHolder fdh = getItem(position);

    String classesText = fdh.getClassesText();      
    if(classesText != "") holder.num_classes.setText(classesText);

    holder.text.setText(fdh.getName());


    // Trigger the download of the URL asynchronously into the image view.
    String imageUrl= "http://graph.facebook.com/" + getItemId(position) + "/picture?type=square";

    Picasso.with(context)
    .load(imageUrl)
    .placeholder(R.drawable.fb_default)
    .into(holder.image);

    return view;
}

我无法弄清楚的有趣的事情是我的图像/名称都没有混淆,而只有Classes文本。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

if(classesText != "") holder.num_classes.setText(classesText);// this is the problem, 

如果您为项目1设置值为“test”并且第10项重用视图,如果classesText为空或null,则不更新该值,因此它将显示第10项的“test”还有一个其他案例来设置空文本或默认值

if(classesText != "") holder.num_classes.setText(classesText); else {holder.num.classes.setText("")}

答案 1 :(得分:1)

你应该替换这个

if(classesText != "") holder.num_classes.setText(classesText);

使用

if(classesText != "") {holder.num_classes.setText(classesText);} 
else {holder.num.classes.setText("")}

答案 2 :(得分:0)

试试这种方式

@Override
    public View getView(int position, View view, ViewGroup parent) {

          ViewHolder holder;
            if ((view == null) || (view.getTag() == null)) { // UPDATE HERE
                view = LayoutInflater.from(context).inflate(R.layout.friend_list_item, parent, false);
                holder = new ViewHolder();
                holder.image = (ImageView) view.findViewById(R.id.friend_pic);
                holder.text = (TextView) view.findViewById(R.id.friend_name);
                holder.num_classes = (TextView) view.findViewById(R.id.shared_classes);         
                holder.text.setTypeface(face);
                holder.num_classes.setTypeface(lightface);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            // Build Shared Classes text
            FriendDataHolder fdh = getItem(position);

            String classesText = fdh.getClassesText();      
            if(classesText != "") holder.num_classes.setText(classesText);

            holder.text.setText(fdh.getName());


            // Trigger the download of the URL asynchronously into the image view.
            String imageUrl= "http://graph.facebook.com/" + getItemId(position) + "/picture?type=square";

            Picasso.with(context)
            .load(imageUrl)
            .placeholder(R.drawable.fb_default)
            .into(holder.image);

            view.setTag(holder); // updata here

            return view;



    }