gpt4 book ai didi

android - ListView 中的切换按钮点击自身

转载 作者:行者123 更新时间:2023-11-29 00:21:44 25 4
gpt4 key购买 nike

您好,我正在尝试创建一个应用程序,让您可以从 ListView 中选择兴趣,我创建了自己的带有 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

问题:-
涂鸦时为什么会自己点击?

谢谢

最佳答案

这是因为在 Listview 中重用了 Views..这里 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..

像往常一样继续保持并尝试..

关于android - ListView 中的切换按钮点击自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22530167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com