在列表视图中第一项是显示不正确 [英] First item in list view is not displaying correctly

查看:149
本文介绍了在列表视图中第一项是显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林做一点购物清单应用程序。在应用中,如果列表视图中的项目已被标记为买它被用线划掉。我的问题是,当第一次显示的列表中,如果有作为列表购买被标记的任何项目,第一项将即使其不显示为被标记为感兴趣(将具有通过它的线)。

Im making a little shopping list app. In the app, if an item in a list view has been marked as bought it gets crossed off with a line. My problem is that when the list is first displayed, if there are any items that are marked as bought in the list, the first item will appear as being marked as bought(will have a line through it) even if its not.

如果没有项目被标记为买那么第一项显示为它应该

if no items are marked as bought then the first item displays as it should

code为我的阵列适配器

Code for my array adapter

public class ListAdapter extends BaseAdapter{
Context context;
ArrayList<List_Item> items;

public ListAdapter(Context context, ArrayList<List_Item> list){
    this.context = context;
    items = list;
}

@Override
public int getCount() {
    if(items != null)
        return items.size();
    else
        return 0;
}

@Override
public Object getItem(int index) {
    return items.get(index);
}

@Override
public long getItemId(int index) {
    return 0;
}

@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    View view = convertView;
    Holder holder = new Holder();

    if(convertView == null){
        view = LayoutInflater.from(context).inflate(R.layout.complex_list_item, parent, false);
    }

    holder.main = (TextView)view.findViewById(R.id.LItextView1);
    holder.second = (TextView)view.findViewById(R.id.LItextView2);

    List_Item item = items.get(pos);

    holder.main.setText(item.name);
    holder.second.setText(item.qtyToBuy + " " + item.unit + "(s) @ $" + item.price 
                                              + " per " + item.unit.toLowerCase());

    if(item.bought){
        holder.main.setBackgroundResource(R.drawable.strikeout);
    }


    return view;
}

class Holder{
    TextView main;
    TextView second;
}

}

这是怎么回事?我怎样才能解决这个问题?任何建议将是多少AP preciated。

Why is this happening? How can i fix this? Any suggestions would be much appreciated.

推荐答案

的问题是,在convertView得到为列表中的每个项目重复使用。
您正在创建一个新的各一次,但你分配给它的值从视图中引用。

The problem is that the convertView gets reused for each item in the list. You're creating a new holder each time, but you're assigning it values referenced from the view.

那么,什么情况是,其中一个条目,你叫这一点,并设置背景图片 -

So what happens is that for one of the entries, you call this and set the background image -

if(item.bought){
    holder.main.setBackgroundResource(R.drawable.strikeout);
}

这不只是设置背景图片为当前列表项的条目 - 这是设置在的TextView 就是得到重用每个项目的背景图像。您需要重置每次使用 - 而背景图片在引用的TextView 从该点设置。喜欢的东西 -

This isn't just setting the background image for the current list item entry - it's setting a background image on the TextView that's getting reused for each item. And that background image is set in the referenced TextView from that point on - you need to reset it for every use. Something like -

if(item.bought){
    holder.main.setBackgroundResource(R.drawable.strikeout);
} else {
    holder.main.setBackgroundResource(R.drawable.normal);
}

这篇关于在列表视图中第一项是显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆