Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Christopher Coudriet
196 PointsAbility to search the editFriendsActivity and retain the checked friends when filtering.
I am have been for a fews trying to implement properly a way to search / filter through the editFriendsActvity of the Android self destruct message app.
So far i have got the search working but when the list is filtered it loses / does not retain which friends are checked.
I also have a way to get which friends are checked and there position prior to filtering but once filtering starts I cant figured out a way to let the adapter know which friends should be selected / checked. I have been searching the internet for days on a solution for this but have come up empty hopefully somebody can provide a working solution.
6 Answers
Ben Jakuben
Treehouse TeacherCan you paste in the code you're using so far?
Christopher Coudriet
196 PointsHi Ben thanks for the response! Currently for the EditFriendsActivity I am using the same code as the tutorial as I have removed things I was trying because they weren't working. Currently I can get the search functionally with ease but retaining whats checked and unchecked after filtering the list is what I can't figure out. I mean I had a way of getting whats checked prior to filtering by using the below method from the RecipientsActivity. But once again getting that data back into the adapter or somehow letting the adapter know is the challenge. Hopefully you understand what I am talking about and can provide a solution. Thanks in advance.
protected ArrayList<String> getRecipientIds() {
ArrayList<String> recipientIds = new ArrayList<String>();
for (int i = 0; i < getListView().getCount(); i++) {
if (getListView().isItemChecked(i)) {
recipientIds.add(mFriendsFinal.get(i).getObjectId());
}
}
return recipientIds;
}
Ben Jakuben
Treehouse TeacherLet's start by making sure I understand correctly with a simple example. Let's say the list has 3 items to start, and the 2nd item (index 1) is checked. If you filter and now only that one item is showing, it's at position 0 in the list, and you want the checkmark to show.
I don't know for sure, but you might be able to store the positions of checked items using getCheckedItemPositions before filtering and then check against that to check/uncheck items in the list. I'm having trouble thinking of a good example right now.
We discussed a similar issue in the Forum a while back: https://teamtreehouse.com/forum/listview-search
Christopher Coudriet
196 PointsBen once again thanks for your response! Yes that is EXACTLY what I am talking about. I will check out the link in the forum to see if that helps. But yes I need a way to check that list of check items once filtering ends.
Christopher Coudriet
196 PointsOk, so I checked out that forum you suggested and it does not help as that relates to getting the position of clicked item for pushing to another intent. What I need is a way to retain the checked positions after filtering a listview.
Paul Stevens
4,125 PointsHello,
I don't have an answer with code, but would it be possible after filtering the editfriends list to run down the list of people in the filtered list view and compare their names/ids with the names/ids that are in your friends list, if there is a match then check the name.
Something like this (Not code, sorry, just in English)
Filter the EditFriends list;
Loop through filtered list one name at a time; Loop through my friends list for each name in the filtered list; If (filtered name) is equal to (friend name) then check name at this position next loop next loop
I don't know how the code should be in java for this, but the idea should be ok. This may not be the most effective way to do it though.
Hopefully may give a bit off direction or an idea, I know it's not really an answer. Can I see your filtering method? Maybe I could paste it into my app and then play around a bit. I am beginning to modify my Ribbit app and this sounds useful :D
Paul
Christopher Coudriet
196 PointsThanks Paul. I ended up figuring it out by doing some things a little differently.