Android的ListView的项目选择和复位 [英] Android ListView Items select and reset

查看:160
本文介绍了Android的ListView的项目选择和复位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的简单应用程序的问题。它使用了一个列表视图:
- 打开一个新的活动时pressed和它的上视图模式
- 高亮显示所选项目时,它在编辑模式

I've a problem with my simple application. It uses a listview that has to: - open a new Activity when pressed and it's on view mode - highlight the selected item when it's on edit mode

我做以下内容:

        ListView lv = (ListView)findViewById(R.id.categoryListView);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {

                String entry = (String) parent.getItemAtPosition(position);

                if (_editMode)
                    view.setBackgroundColor(Color.parseColor("#5B5B5B"));
                else 
                {
                    Intent intent = new Intent(MainActivity.this, CategoryActivity.class);
                    intent.putExtra("CATEGORY", entry);
                    startActivity(intent);
                }
            }
        });

然后我想,当我打开查看模式下,所有项目必须取消,而我这样做:

Then I want that when I turn to view mode, all items must be deselected, and I do this:

for (int i = 0; i < lv.getAdapter().getCount(); i++)
{
    lv.getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));
}

不过这只有在列表视图中的所有项目都可见做工精细。
我试着在适配器上实现此没有成功...

But this is working fine only if all the items in listview are visible. I've tried implementing this on the adapter without success...

任何建议?

谢谢!

修改

好吧贾瓦德回答后,我只是想出了如何做getView的方式工作。所以这是我用过的解决方案:

Ok after Jawad answer I just figured out how does "getView" method work. So this is the solution I've used:

我声明包含所选项目的ArrayList

I declared an arraylist containing selected items:

ArrayList<String> itemSelected = new ArrayList<String>();

这是在ListView onItemClick监听器(在这里您可以选择和取消选择的项目):

This is the ListView onItemClick listener (where you can select and deselect items):

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {


                String entry = (String) parent.getItemAtPosition(position);

                if (itemSelected.contains(entry))
                {
                    itemSelected.remove(entry);
                }
                else
                {
                    itemSelected.add(entry);
                }

                ((ArrayAdapter<String>)lv.getAdapter()).notifyDataSetChanged();
        }
    });

这是getView方法ovverride:

This is the ovverride of getView method:

itmList.setAdapter(new ArrayAdapter<String>(this,R.layout.list_black_text,R.id.list_content, itmStrList){
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if (convertView != null)
            {
                if (itemSelected.contains(itmList.getAdapter().getItem(position))) {
                    convertView.setBackgroundColor(Color.RED);
                } else {
                    convertView.setBackgroundColor(Color.parseColor("#5B5B5B"));
                }
            }

            return super.getView(position, convertView, parent);
        }
    });

这是怎么取消所有项目:

And this is how to deselect all items:

        itemSelected.clear();

        ((ArrayAdapter<String>)lv.getAdapter()).notifyDataSetChanged();

感谢你的男人:)

推荐答案

我觉得你的问题是你有没有注意查看回收。当你正在改变视图的背景颜色,视图可以回收,你将有一些不希望的。勾选此链接了解详情。

I think your problem is your are not paying attention to view recycling. When you are changing the color of a view's background, the view can be recycled and you will have something not desired. Check this link for details.

您应该有一个,可以说布尔变量,在你所谓的像isSelected underlyning数据。而在你的getView()方法。

You should have a, lets say boolean variable, in your underlyning data called something like isSelected. And add this code in your getView() method.

    if(item.isSelected()){ 
            setBackgroundColor(Color.parseColor("#5B5B5B"));
    }
    else{
           setBackgroundColor(Color.parseColor("#FFFFFF"));
    }

然后在你的onItemClick通过添加

Then in your onItemClick add replace view.setback... by

    lv.getAdapter().getItem(position).setSelected(true);
    lv.getAdapter().notifyDataSetChanged();

您可能需要一个转换。

最后改变这种

for (int i = 0; i < lv.getAdapter().getCount(); i++)
{
    lv.getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));
}

这样:

for (int i = 0; i < lv.getAdapter().getCount(); i++)
{
    lv.getAdapter().getItem(i).setSelected(false);
}

lv.getAdapter().notifyDataSetChanged();

为什么你的code的部分仅如果所有的项目都可见的原因是鉴于回收利用。此外lv.getChildAt()只给你可见的看法。然后,您的code可能会崩溃,因为adapter.getcount也许更大然后列表视图孩子的数量。

The reason why your portion of code works only if all the items are visible is view recycling. Moreover lv.getChildAt() gives you only the views that are visible. Your code may then crash because adapter.getcount maybe bigger then the number of listview childs.

希望它帮助。

这篇关于Android的ListView的项目选择和复位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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