Reputation: 491
I've started learning Android programming about a month ago and am making some good progress. But I do have a basic question regarding Activities and Adapters.
I have an activity with a listview and I am using an adapter to "populate" the rows of the listview. My adapter inflates a layout with a checkbox in it.
Given this situation, if I want to click a button in the action bar menu (like an OK button), capture all the checked items and send it to an activity, how do I do it?
I ask this question as I am not able to figure out how the Activity and the Adapter communicate with each other. From many examples I've seen, the checkbox listener seems to be within the adapter while the Ok button is part of the menu layout inflated in the Activity and hence the button click listener is within the activity.
So in such a case, how can I get the full list of checked items on clicking the OK button?
Upvotes: 1
Views: 84
Reputation: 31
First keep in mind that,whenever you want to implement you need to create 5 files. create an activity which contains,
Now,take an onclick listner event on ok buttton and inside that put if condition that shows checkbox is checked or not using ischecked() method.if chechbox is checked then it will show in next activity with using on click event of ok button.
In java bean class you need to take variable if checkbox is checked then its value will be true and if checkbox is unchecked then that value will be false.
Upvotes: 1
Reputation: 7586
Your Activity can hold a reference to the Adapter if you create your Adapter as an instance variable of your Activity. Use this to your advantage.
When you check or un-check things and get notified in your adapter you can keep track of things that are checked in a set or whatever data structure you prefer. When you hit the OK button in your Activity, you can ask your Adapter for what's checked by adding a method to your adapter that returns this information.
Upvotes: 2