質問

Hi I am trying to create an app that lets you select interests from an listview, I created my own custom adapter with a textView and toggle button when clicked on a toggle button that particular value is added to shared preferences to remember for later use, the problems i am facing are:

1) when i click the toggle button of a particular item of list another also gets clicked
2) when i scrawl up and down in the list automatically other items are clicked

userInterestList is the shared preference list which is initially empty and interestList_Item is also a shared preference string array which is already initialized

    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;
    }

Logs onstart:-

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

question:-
why first 7 list items showed so many times instead of all list items?

Logs on click toggle:-

    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

question:-
why are 2 items being selected when one is being clicked ?

Log when scrawling:-

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

question:-
when scrawling why is it clicking on its own?

thank you

役に立ちましたか?

解決

Its because of reusing the Views in Listview..Here setChecked() method calls onCheckedChangeListener..so before setChecked() we make listener to null..after that we again set the listener.. change this line

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

into

  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..

keep remaining as usual and try..

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top