views:

108

answers:

1

Hello,

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!

+1  A: 

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.

Falmarri
As I mentioned, my choice_mode is not set (which means set to none) because when I use lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); I get what happens on my previous question: http://stackoverflow.com/questions/4010623/android-click-on-listitem-checks-wrong-checkbox - when checking a checkbox on a certain line, it check a checkbox on a different line... Any solution for that? I guess this will solve all my problems :\
liorry
I updated my answer, try something like that maybe. In your first one though. You have to set the mode to multi_choice
Falmarri
no... same problems. if I don't set choice mode I get noting (or force close for Null) and if I set lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); I get crazy checkboxing... :\
liorry
Post some more code. How are you binding your listview to your adapter?
Falmarri
setListAdapter(new ImageCursorAdapter(this, android.R.layout.simple_list_item_checked, cur, displayFields, displayViews));and you can see the custom adapter here: http://stackoverflow.com/questions/4010623/android-click-on-listitem-checks-wrong-checkbox
liorry
Can you update your question with the definition and delcaration of cur, displayFields and displayViews
Falmarri
Thanks for replying! edited the main question.
liorry
Any ideas Falmarri? :\
liorry
I updated my answer. I'm sort of at a loss. `android.R.layout.simple_list_item_single_choice` Instead of using that make your own layout
Falmarri
Thank you very much for all your help!Tried that already (and again now...) same result - dancing checkboxes :\ I'm trying to understand how I can use what Romain Guy said here: http://groups.google.com/group/android-beginners/browse_thread/thread/7372b92dfa50ee33/70d0eb8f51ea8b26?#70d0eb8f51ea8b26 (the exact same issue as I got)...
liorry
At this point I think you should just roll your own check/uncheck functionality.
Falmarri
Yeah... saw your update... I'll think this through... maybe rewrite some code :\Thank you once again!
liorry