Search code examples
androidlistviewcheckedtextview

Android CheckedTextVIew Clear All/Select All


I have a ListView that utilizes a CheckedTextView and a clear all button that has the following code:

btnClearAll.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            int listsize = songsList.size();
            filelv = (ListView)getView().findViewById(R.id.FileList);
            checkedCount = 0;
            for (int i=0; i<listsize-1; i++) {
                // if(currentPlayList.get(i).equals ("Checked")){
                songsList.get(i).get("songCheckedStatus").equals("Not Checked");

                filelv.setItemChecked(i, false);
            } 

        }       
    });

When the code executes, each array "songsList" value is set to "Not Checked" correctly, so I know that the button is working. However, the CheckedTextView items are not "unticking".

Where am I going wrong?


Solution

  • olution:

    As mentioned above, I just set up a global variable and passed whether I wanted to Select All or Clear All to it. Then I just called my function for populating the ListView. As the ListView is built, it checks the variable in a simple "if" statement, and adds whether to check or uncheck. The cursor adapter then does the check/uncheck as it reruns.

    //The code for clearing all.
    btnClearAll.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                checkedCount = 0;
    
                GetMusicMode = "ClearAll";
                GetMusic getMusic = new GetMusic();
                getMusic.execute();
            }       
        });
    

    And this from my data grabber for the ListView.

     ....
     String track_Title = null;
     String track_Path = null;
     String track_Artist = null;
     String track_Album = null;
     String track_ID = null;
     String track_TrackNumber = null;
     String track_AlbumID = null;
     String track_ArtistID = null;
     String track_Checked = null;
    
     if (Constants.GetMusicMode.equals("SelectAll")){
    track_Checked = "Checked";
    }else{
    track_Checked = "Not Checked";
     }
     .....
    
     .....
    
     HashMap<String, String> song = new HashMap<String, String>();
    song.put("songTitle", track_Title);
    song.put("songPath", track_Path);
    song.put("songArtist", track_Artist);
    song.put("songAlbum", track_Album);
    song.put("songTrackNumber", track_TrackNumber);
    song.put("songID", track_ID);
    song.put("songAlbumID", track_AlbumID);
    song.put("songArtistID", track_ArtistID);
    song.put("songCheckedStatus", track_Checked);
    
    
    // Adding each song to SongList
    songsList.add(song);
    
        ....throw to cursor adapter
    

    and finally, this section from the cursoradapter getview

    if (songsList.get(position).get("songCheckedStatus").equals("Checked")){
    holder.checkTextView.setChecked(true);
    }else{
     holder.checkTextView.setChecked(false); 
    }