listview中的切换按钮单击自身

时间:2014-03-20 10:32:39

标签: android listview android-listview sharedpreferences togglebutton

您好我正在尝试创建一个允许您从列表视图中选择兴趣的应用程序,我在单击切换按钮时创建了我自己的带有textView和切换按钮的自定义适配器,将特定值添加到共享首选项以便以后记住使用, 我面临的问题是:

1)当我点击列表中特定项目的切换按钮时,另一个也被点击了 2)当我在列表中自动向上和向下潦草地写下其他项目时

userInterestList是共享首选项列表,最初为空,且interestList_Item也是已初始化的共享首选项字符串数组

    public class Adapter extends BaseAdapter {
    int i=getCount()-1;
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return interestList_Item.length;
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return interestList_Item[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

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

        if(convertView==null)
        {
            LayoutInflater inflater = (LayoutInflater) InterestsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.interest_list_item, parent,false);
        }
        final TextView userName = (TextView)convertView.findViewById(R.id.profile_name);
        final ToggleButton toggle = (ToggleButton)convertView.findViewWithTag("toggle");
        Log.d("","userInterestList : "+userInterestList+"  interestList_Item:"+interestList_Item.toString());            
        if(userInterestList.contains(interestList_Item[position]))
             toggle.setChecked(true);
        toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method 
                if(isChecked){
                    if(userInterestList.equals(""))
                        userInterestList = interestList_Item[position];
                    else
                        userInterestList = userInterestList+",new"+interestList_Item[position];
                    _appPrefs.saveUserInterestList(userInterestList);
                        Log.d("","check position :- "+position+" "+userInterestList+" "+interestList_Item[position]);
                } else {
                        userInterestList = userInterestList.replace(interestList_Item[position], "");
                        _appPrefs.saveUserInterestList(userInterestList);
                        if(userInterestList.contains(",new,new"))
                             userInterestList = userInterestList.replaceAll(",new,new",",new");
                        _appPrefs.saveUserInterestList(userInterestList);
                        Log.d("","uncheck position :- "+position+" "+userInterestList+" "+interestList_Item[position]);
                }
            }
        });
        userName.setText(interestList_Item[position]);
        Log.d("","num :"+i+"     username :"+userName.getText()+"        position:"+position);
        return convertView;
    }

登录开始: -

num :14     username :Cars        position:0
num :14     username :Business        position:1
num :14     username :Drinking        position:2
num :14     username :Travelling        position:3
num :14     username :Making recycled paper        position:4
num :14     username :Animal care        position:5
num :14     username :Organic farming        position:6
num :14     username :Cars        position:0
num :14     username :Business        position:1
num :14     username :Drinking        position:2
num :14     username :Travelling        position:3
num :14     username :Making recycled paper        position:4
num :14     username :Animal care        position:5
num :14     username :Organic farming        position:6
num :14     username :Ice skating        position:7
num :14     username :Cars        position:0
num :14     username :Business        position:1
num :14     username :Drinking        position:2
num :14     username :Travelling        position:3
num :14     username :Making recycled paper        position:4
num :14     username :Animal care        position:5
num :14     username :Organic farming        position:6

问题: -
为什么前7个列表项显示了这么多次而不是所有列表项?

登录点击切换: -

    check position :- 0 Cars Cars
check position :- 6 Cars,newOrganic farming Organic farming
num :14     username :Cars        position:0
num :14     username :Business        position:1
num :14     username :Drinking        position:2
num :14     username :Travelling        position:3
num :14     username :Making recycled paper        position:4
num :14     username :Animal care        position:5
num :14     username :Organic farming        position:6

问题: -
为什么在点击一个项目时会选择2个项目?

抓取时记录: -

num :14     username :Racing        position:8
num :14     username :Cars        position:0
num :14     username :Ice skating        position:7
num :14     username :Racing        position:8
num :14     username :Hunting        position:9
num :14     username :Gymnastics        position:10
num :14     username :Painting        position:11
num :14     username :Video gaming        position:12
num :14     username :Fishing        position:13
num :14     username :Swimming        position:14
num :14     username :Racing        position:8
num :14     username :Ice skating        position:7
check position :- 14 ,newOrganic farming,newSwimming Swimming
num :14     username :Organic farming        position:6
num :14     username :Animal care        position:5
num :14     username :Making recycled paper        position:4
num :14     username :Travelling        position:3
num :14     username :Drinking        position:2
num :14     username :Business        position:1

问题: -
在潦草时为什么单击它?

谢谢

1 个答案:

答案 0 :(得分:2)

这是因为重用了Views中的Listview ..这里setChecked()方法调用onCheckedChangeListener ..所以在setChecked()之前我们会监听null之后我们又设置了监听器.. 改变这一行

 if(userInterestList.contains(interestList_Item[position]))
         toggle.setChecked(true);

进入

  toggle.setOnCheckedChangeListener(null);
    if (userInterestList.contains(interestList_Item[position])) {
        toggle.setChecked(true);
    } else {
        toggle.setChecked(false);
    }
    //Here again set the listener as in your code..

继续像往常一样继续尝试..