• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Show/Hide checkboxes in Custom Adapter

 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
I want to show/hide checkboxes in my custom adapter but i couldn't find any solution for 3 days of search.

In my custom adapter all checkboxes are hidden as default when user click show button then make them visible also click again invisible them.
Here is my code.

Thanks for any help

Main Activity


Custom ListView


And my Custom Adapter


Screen shots are here
show.png
[Thumbnail for show.png]
hide.png
[Thumbnail for hide.png]
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't clear what the problem is. You state what you want and show screen shots of the behavior. But you don't state what the problem is. What isn't happening or what error do you get?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear steve
Thanks for reply.

The problems are:

1 - i cannot Show/Hide checkboxes
2 - I cannot check/uncheck unboxes when state is visible
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am on my phone so I can't do a lot of research but for making the check boxes visible, when you press the show hide button you need to update the list view to tell it to recreate the views. The best way to do it is to tell the adapter its data has updated. If you read the api for adapters the method call should present itself.

For the other problem. .. This might be tougher. One option would be to switch the behavior of the on item selected listener for the list and have it toggle the checked state of the check box and underlying variable. There may be another option related to creating a pass through for click events, the Android developers network YouTube channel might have a video that covers this if I recall.
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.
I have solved my problem #1 i am going to write solution here who may help someone's hours and hairs.

I have created new public method and call it when show/hide button clicked.

Now i am still researching how to handle check/uncheck all and i still need idea and help
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ibrahim yener wrote:Thanks for reply.
I have solved my problem #1 i am going to write solution here who may help someone's hours and hairs.

I have created new public method and call it when show/hide button clicked.

Now i am still researching how to handle check/uncheck all and i still need idea and help


I think this work starts out in your adapter. You should add a few methods to the adapter to check/uncheck individual items, and view their checked status. This is an example:


The first method sets the toggle state for the PhoneListView item, and then puts it in the list of checked items if it is toggled on (and isn't already in the list) and takes it out of the list if item is toggled off. The second method just checks the state of the toggle (since I can't stand the asymetry of setting the checked stated but not getting it... this method is not technically needed...). Then you have a simple 'check all' method which I would also put in the adapter class:

And this code would be called from your onClick listener for the 'Select All' button.
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Steve
Thanks for help and code. I really appreciate for that.

Could you tell me how can i call setAllItemsCheckedState method (or any method) inside of an adapter.

So far i know, codes execute top to bottom.

What can i put into my method named public void checkUncheckAll(View v) in my MainActivity.java class to call adapter.
Also, i don:t know why but most of my application doesn't update adapter for adapter.setNotifyOnChange(true);

Seriously, i need to learn and custom adapter things.

Thank you for your help again. And sorry for my English it is almost 1am here.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ibrahim yener wrote:Could you tell me how can i call setAllItemsCheckedState method (or any method) inside of an adapter.

So far i know, codes execute top to bottom.


I may be mis-reading your question, I am sorry if I am. But you would call the method like any other method on any other object. You need a reference to an instance of the adapter, then you call the method from it (adapter.setAllItemsCheckedState(true) to toggle them on for example). It is just like calling myArrayList.size(). Code in a method is executed top to bottom, unless there is something that modifies the execution order (like if statements or loops). But methods are executed when they are called, their order is irrelevent.

So your strategy would be to add an onTouchListener() to your Select All/Clear All button. In that onClick method, get a reference to the adapter (your Activity already has one). Then call the setAllItemsCheckedState(boolean) method with the correct state (true or false) depending on if the items should be checked or unchecked.
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Steve
I don't how to thank you. I really appreciate for your help.
All i have to do after calling method inside of adapter just adapter.setNotifyOnChange(true); isn't it?

Have a great day.

By the way, you had 20 cows yesterday and today 17 left. What happened?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yessssssssssssss
Finally i did it. I am able to check/uncheck all checkboxes.

Steve, I want to thank you for help. Probably i couldn't succeed without your guidance.

soon I am going to write tutorial for the solution.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Glad you got it worked out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic