当标志着ListView控件中的项目(改变背景颜色),它是重复其他项目 [英] When marking an item (changing background color) in ListView it's repeating for other items

查看:103
本文介绍了当标志着ListView控件中的项目(改变背景颜色),它是重复其他项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想庆祝我做以下code中的第二项:
这code是从我的适配器,扩展ArrayAdapter:

If I want to mark the second item I'm doing the following code: This code is from my Adapter that extends ArrayAdapter :

if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.channel_list, null);
    } 

    MyContent o = items.get(position);
    if (o != null) {
        TextView tt = (TextView) convertView.findViewById(R.id.toptext);
        TextView bt = (TextView) convertView.findViewById(R.id.bottomtext);
        if (tt != null) {
            tt.setText(o.Top());                            
        }
        if(bt != null){
            bt.setText(o.Bottom());
        }
        if(position == 2) {
            convertView.setBackgroundColor(R.color.selectem_color);
        }
    }
    return convertView;

它会显示在列表视图,但这个项目(该项目11'th和13'th等)后,每个标记项9'th

It will show the list view but mark every 9'th item after this item (the 11'th item 13'th and so on).

有谁知道是什么原因?

推荐答案

有两种情况,其中getView方法可以被调用。如果converView为空,你必须创建一个新的视图。如果不是空留下屏幕因为用户滚动的循环并返回到您的方法可以重复使用的项目。

There are two cases in which the getView method can be called. If converView is null you have to create a new View. If it is not null an item that left the screen because of the user scrolling is recycled and returned to your method to be reused.

这对象是在列表之前显示的对象。你必须检查其状态和它的每一个属性设置为你想要它具有的价值。你不能像的对象是新的您得到标记和没有标记过的对象。不要在你的getview方法是这样的。

This object is an object that was shown in the list before. You have to check its state and set every property of it to the value you want it to have. You can't act like the object is new you get marked and not marked objects back. Do something like this in your getview method.

if(item is selected) {    
    convertView.setBackgroundColor(selected color);
} else {
    convertView.setBackgroundColor(not selected color);
}

在您的code中的,如果的其他情况下丢失。

In your code the else case of the if is missing.

这篇关于当标志着ListView控件中的项目(改变背景颜色),它是重复其他项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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