Question

I've created a custom ListView by extending SimpleCursorAdapter. The result is IMAGE + CheckedTextView (Text + Checkbox).

After I had issues with the wrong checkboxes getting checked (see here) I had to remove lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); from onCreate.

Now I click on a ListItem line and it checks the right checkbox.

What I'm trying to do is make a LinearLayout.VISIBLE when 1 or more checkboxes are checked AND make LinearLayout.GONE when no checkbox is checked.

Here is the code:

lv.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

CheckedTextView markedItem = (CheckedTextView) view.findViewById(R.id.btitle);

if (!markedItem.isChecked()) {
 markedItem.setChecked(true);

 //show bottom control panel
 findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.VISIBLE);


} else {
 markedItem.setChecked(false);

 SparseBooleanArray lala = ((ListView) parent).getCheckedItemPositions();

 //if no checkbox is checked, hide bottom control panel
 if (lala == null) {
  findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.INVISIBLE);
 }
}
view.refreshDrawableState();
//showSortBtn(markedItem);

} });

This is how I bind the listView to the custom adapter:

projection = new String[] { Browser.BookmarkColumns._ID,
                Browser.BookmarkColumns.FAVICON, Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.URL };
        displayFields = new String[] { Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.FAVICON, Browser.BookmarkColumns.URL };
        int[] displayViews = new int[] { android.R.id.icon, android.R.id.text1 };

        cur = managedQuery(BOOKMARKS_URI, projection,
                Browser.BookmarkColumns.BOOKMARK + " == 1", null, Browser.BookmarkColumns.VISITS + " DESC");

        setListAdapter(new ImageCursorAdapter(this,
                android.R.layout.simple_list_item_single_choice, cur,
                displayFields, displayViews));

I have two issues with this:

  1. if I check 2 or more checkboxes and then uncheck only one, the LinearLayout disappears which means SparseBooleanArray lala = ((ListView) parent).getCheckedItemPositions(); is null. why is it null if there are still other checkboxes checked?

  2. if I switch findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.INVISIBLE); to findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.GONE); (like I want to do) the first checkbox is being checked correctly and after the LinearLayout is GONE, everything is a mess again, just like on my previous question.

Appreciate any help!

Was it helpful?

Solution

Why are you expecting getCheckedItemPositions() to return null if there aren't checked items? It looks like your choice mode is wrong

A SparseBooleanArray which will return true for each call to get(int position) where position is a position in the list, or null if the choice mode is set to CHOICE_MODE_NONE.

The array should only be null if it's choice_mode_none.

Why not try something like this?

if (((ListView) parent).getCheckedItemPositions().size() > 0){

    findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.VISIBLE);
else{

    findViewById(R.id.bottom_control_bar).setVisibility(LinearLayout.GONE;
}

Update

I'm pretty sure it has to do with this line

 View v = inView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.image_list, null);
        }

You get the view, then reassign the view to the inflater. I'm kind of at a loss though at exactly why it would give the behavior you're exeriencing. You might also want to try the bindView() method though I'm not sure why. Maybe instead of using the default android layout (the one you're giving to the setAdapter, make your own layout. It clearly is getting confused as to which view is assigned to which layout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top