Levan Voronin
Levan Voronin

Reputation: 107

How to show/hide check boxes in ListView programmatically

I have next problem: I use following adapter to populate ListView:

public class PlaylistCursorAdapter extends CursorAdapter{

public PlaylistCursorAdapter(Context context, Cursor cursor){
    super(context, cursor, 0);
}

public static class ViewHolder{
    CheckBox checkBox;
    TextView playlistTitle;

    public ViewHolder(View view){

        checkBox = (CheckBox)view.findViewById(R.id.checkBox);
        playlistTitle = (TextView)view.findViewById(R.id.playlistTitle);
    }
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    View view = LayoutInflater.from(context).inflate(R.layout.playlist_item, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(view);
    view.setTag(viewHolder);
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    ViewHolder viewHolder = (ViewHolder)view.getTag();
    viewHolder.checkBox.setVisibility(View.GONE);

    int playlistName = cursor.getColumnIndex(MediaStore.Audio.Playlists.NAME);
    String playlist = cursor.getString(playlistName);

    viewHolder.playlistTitle.setText(playlist);
}
}

As you can see, I have checkbox in the row which is gone in the very beggining and what I want to do is to show all of them(checkboxes) when onItemLong is performed.

Actually I already implemented onItemLong in my activity and I was nearly done with that task with following code:

 public void showCheckBoxes(){
    for(int i = 0; i != playlist.getAdapter().getCount(); i++) {

        mCheckBox = (CheckBox) playlist.getChildAt(i).findViewById(R.id.checkBox);
        mCheckBox.setVisibility(View.VISIBLE);
        getActivity().invalidateOptionsMenu();
    }
}

The method works fine until you can see all items in ListView on a screen. But then I noticed that (mCheckBox = (CheckBox) playlist.getChildAt(i).findViewById(R.id.checkBox);) - is actually new problem, because if my listview is longer than screen, I always get the following error - (java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference) and then app crashes.

Yep, I understand why is it happens, but have no idea how to solve the problem.

Will really appreciate if someone will give me answer, hint, link or some idea how to solve the problem.

Is it somehow possible to change code to show/hide checkboxes. Maybe there is a way to do that without calling .findViewById all the time.

Thank you in advance.

Upvotes: 1

Views: 698

Answers (1)

android_griezmann
android_griezmann

Reputation: 3887

Do the following

public class PlaylistCursorAdapter extends CursorAdapter{

public boolean isAllItemsVisible;

public PlaylistCursorAdapter(Context context, Cursor cursor){
    super(context, cursor, 0);
    }

public static class ViewHolder{
CheckBox checkBox;
TextView playlistTitle;

public ViewHolder(View view){

    checkBox = (CheckBox)view.findViewById(R.id.checkBox);
    playlistTitle = (TextView)view.findViewById(R.id.playlistTitle);
}
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
View view = LayoutInflater.from(context).inflate(R.layout.playlist_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

ViewHolder viewHolder = (ViewHolder)view.getTag();
if(isAllItemsVisible)
    viewHolder.checkBox.setVisibility(View.VISIBLE);
else
    viewHolder.checkBox.setVisibility(View.GONE);
int playlistName = cursor.getColumnIndex(MediaStore.Audio.Playlists.NAME);
String playlist = cursor.getString(playlistName);

viewHolder.playlistTitle.setText(playlist);
}
}

I just take one boolean called isAllItemsVisible to maintain the visibility of the all items.

For showing the all the checkbox you just need to call the following method.

public void showCheckBoxes(){
   mplaylistCursorAdapter.isAllItemVisible=true;
   mplaylistCursorAdapter.notifyDatasetChanged();
}

Try this! Hope it's works for you!

Upvotes: 3

Related Questions