安卓怎么知道选择哪个复选框 [英] Android How to know which check box is selected

查看:293
本文介绍了安卓怎么知道选择哪个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做我的第一个Android应用程序,我有一个问题,我无法找到答案的任何地方对谷歌。

I'm making my first Android application and I'm having a problem for which I can't find the answer anywhere on Google.

我想与复选框项目的列表。我想无论是项目本身的复选框,点击。

I want a list of items with checkboxes. I want both the item itself and the checkbox to be clickable.

    public class MyItem extends ListActivity {
        private ArrayList<MyItem> items;
        public void onCreate(Bundle savedInstanceState) {
            /* code which creates instances of MyItem and inserts them on the *list* variable */
        MyArrayAdapter adapter = new MyArrayAdapter(this, R.layout.my_item, list);

        setListAdapater(adapter);
        setContentView(R.layout.items_list);
    }
        public onListItemClick(ListView l, View v, int position, long id){
            //handles the click on an item
        }

    public class MyArrayAdapter extends ArrayAdapter<MyItem>{
        private MyItem item;
        public MyArrayAdapter(Context context, int resourceId, ArrayList<MyItem> list){
            //code for the constructor
        }
        public getView(int position, View convertView, ViewGroup parent){
            convertView = inflater.inflate(resourceId, null);


            this.item = list.get(position);
            if (this.item == null) {
                return convertView;
            }
            else{
                if (resourceId == R.layout.my_item) {
                    final CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);

                    if(cb != null){
                        //initially
                        if(chosen)
                            cb.setChecked(true);
                        else
                            cb.setChecked(false);
                        //set listener
                        cb.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                if(cb.isChecked())
                                    chosen = true;
                                else
                                    chosen = false;
                            }
                        });
                    }
                }
            return convertView;
        }
    }    
}

不要担心选择的变量。我写了简单的code。它实际上对应于在数据库中的值。在项目上的点击工作得很好。然而,当我点击一个复选框会发生什么情况是这样的:

Don't worry about the chosen variable. I wrote that to simply the code. It actually corresponds to a value in a database. The clicking on an item works just fine. However when I click on a checkbox what happens is this:

  • 在我点击出现选择的复选框(这是Android的用户界面的工作)
  • 在内部被选中该复选框是最后一个画面什么方向,在IE上,如果我我的画面显示8个项目,我点击其中之一(无所谓哪一个)检查出现在正确的复选框,但在内部,第8项是一个得到确认。

我会AP preciate任何帮助,您可以给我。先谢谢了。

I would appreciate any help you could provide me. Thanks in advance.

推荐答案

其实,实施支持选择是关键。安卓确实与列表视图一些优化,使您可以重复使用列表项意见,以避免过多的对象创建和垃圾收集(这往往会导致生涩滚动)。因此,你必须确保,每当有关,你知道哪些列表项目,你正在使用。

Actually, the implementation backing chosen is key. Android does some optimization with list views to allow you to reuse the list item views to avoid excessive object creation and garbage collection (which would often lead to jerky scrolling). As such, you have to make sure that whenever relevant, you know exactly which list item you're working with.

假设你有一个100的列表项。您的屏幕可能不会能够他们来显示所有。你可能只显示十个项目的时间。所以,10次(真查看层次)的创建,以显示这些可见项目。当你向下滚动,而不是创建10个新的意见(共20个),列表中可能只创建到未来十年的项目,多一个(涵盖之一,其中一半一个项目是显示在顶部的情况下,一半是显示在屏幕的共11个项目在屏幕上可见)和物品的其余部分的底部重新使用前创建的视图。

Let's say that you have a 100 list items. Your screen is probably not going to be able to display all of them. You might only show ten items at a time. So 10 views (really view hierarchies) are created to display those visible items. When you scroll down to the next ten items, instead of creating 10 new views (for a total of 20), the list might only create one more (to cover the case where half of one item is showing at the top and half of one is showing at the bottom of the screen for a total of 11 items visible on the screen) and the rest of items reuse the views created before.

因此​​,一个概念表重新presenting第一屏幕可能是这样的:

So a conceptual table representing the first screen might look like this:


Item     View
-------  --------
Item 1   View 1
Item 2   View 2
Item 3   View 3
Item 4   View 4
Item 5   View 5
Item 6   View 6
Item 7   View 7
Item 8   View 8
Item 9   View 9
Item 10  View 10

和滚动下来十余项后,它可能看起来有点像这样的(可能不完全,但是这可以让你的想法):

And for after scrolling down ten items, it might look a little like this (probably not exactly, but this gets you the idea):


Item     View
-------  --------
Item 11  View 1
Item 12  View 2
Item 13  View 3
Item 14  View 4
Item 15  View 5
Item 16  View 6
Item 17  View 7
Item 18  View 8
Item 19  View 9
Item 20  View 10

那么,你可以从这个收集是,当你滚动围绕一个给定的视图可以重新present不同的项目。这意味着你的事件处理程序有一点他们是如何找到他们所涉及到的项目更具活力。

So what you can gather from this is that a single given view can represent different items as you scroll around. This means that your event handlers have to be a little more dynamic in how they find the item they're related to.

这一切是给你一点背景,使你可以改变你如何实现你的 getView 方法。这是你的实际的问题:变量项目是在适配器的范围。不幸的是,我猜你的code,你有没有张贴在这里,你已经换成选择使用项目。您可以设置项目每当一个项目视图被创建。这意味着,创建的第8次后,项目设置为在列表中的第8项。每当你点击一个复选框,您使用的是项目这是对应于您单击列表项视图中的第8个项目,而不是项目。

All this is to give you a bit of background so that you can change how you're implementing your getView method. Here's your actual problem: the variable item is in the scope of your Adapter. Unfortunately, I'm guessing that your code that you haven't posted here that you've replaced with chosen uses item. You set item whenever an item view gets created. This means that after those first 8 views are created, item is set to the 8th item in your list. Whenever you click on a checkbox, you're using item which is the 8th item and not the item that corresponds to the list item view that you clicked.

这里的结构 getView ,我建议你:

Here's the structure for getView that I'd recommend:

public getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.my_item, null);
        }

        final MyItem item = list.get(position);
        final CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);
        // This stores a reference to the actual item in the checkbox
        cb.setTag(item);

        if(item.chosen)
            cb.setChecked(true);
        else
            cb.setChecked(false);

        //set listener
        cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // This gets the correct item to work with.
                final MyItem clickedItem = (MyItem) view.getTag();
                if(cb.isChecked())
                    clickedItem.chosen = true;
                else
                    clickedItem.chosen = false;
            }
        });

        return view;
    }
}

请注意,我已经摆脱了类级别的项目变量。

Note that I've gotten rid of the class-level item variable.

这篇关于安卓怎么知道选择哪个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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