【问题标题】:toggle button in listview clicking on itself列表视图中的切换按钮单击自身
【发布时间】:2014-04-27 03:11:00
【问题描述】:

您好,我正在尝试创建一个应用程序,让您从列表视图中选择兴趣,我创建了自己的自定义适配器,当单击切换按钮时,我使用 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

问题:-
为什么单击一个项目时会选择两个项目?

潦草时记录:-

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

问题:-
潦草时为什么会自己点击?

谢谢

【问题讨论】:

    标签: android listview android-listview sharedpreferences togglebutton


    【解决方案1】:

    这是因为在Listview..这里重用了ViewssetChecked()方法调用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..
    

    像往常一样保持状态并尝试..

    【讨论】:

    • 哇直射...成功了!!!但你能解释一下这些观点还是没有弄错我的错误......非常感谢!
    • @user3230689 getView() 方法返回已经使用的视图..这样视图已经有一个监听器..setChecked 方法调用 onChecked 更改监听器,如果它不为空。这就是你的方法调用的原因..您正在使用 if(Convertivew==null) 之类的条件创建视图,这意味着 converview not null 重用旧视图..
    • @kalyanpvs:有类似的问题。你能看看这个吗? stackoverflow.com/questions/31169131/…非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2017-07-24
    相关资源
    最近更新 更多