【发布时间】: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