Show / Hide Listview SectionIndex on demand - android

i implemented a listview which implements SectionIndexer ...
everything fine so far.
Normally the items are sorted by Name, but i also offer the option to sort the list in a different way - by distance (from the user to the objects).
So, when the list is sorted the 2nd way, i want to hide the previously generated SectionIndex.
I'm just not able to do so.
I tried, re-writting most of the methods,
I tried it with a separation in the Constructor (clear why it doesnt work, it doesnt get called a second time)
I even tried it with implementing a second listadapter, and just using a different one? Even in this case the SEctionIndex is shown! I really don't understand this one.
So would be really great, if anyone knows whats going on :)
thanks a lot, mike

Your observations are correct. Let me tell you first why the constructor never gets called the second time. SectionIndexer are a special kind. They create the index only once for a particular set of data and re-use them on that adapter. The bigger issue which I had come across was when the underlying data changed for the adapter, the sectionIndexer still remained the same.
Check my Question and the answer there.
Coming back to your query here.
If you change the orientation after selecting the second option, you would observe that the constructor will get called and you will be able to re-populate the sectionIndex again. So basically you need to call onSizeChanged again and get the sectionIndex repopulated.

When you Short your List with different way ,you have a two option to load again .
after filled those new collection for adapter
1) you can make a notify this adapter .
2) you can fill set adapter again .

If by SectionIndexer which remains displayed you mean the section overlay you can achieve this by calling setFastScrollEnabled(false) before to switch to your other listadapter which does not implements SectionIndexer.

Related

Android: how to detect when items are successfully loaded in ListView?

I am using ListView which gets populated by the use of Adapter.
One of its features is also highlighting position on the list for the user with listView.getChildAt(savedViewPosition).setSelected(true).
At the conformation change, I would like to retaing poisiton of the selected item.
However, while I have tried several options, the best so far was to set a delay with .postDelay() and wait for the listView to load.
With this in mind, I would ask if there is any call that would enable me to wait until the items are loaded/visible and then posting the method immediately?
This sounds like something that could be accomplished by overriding notifyDataSetChanged() and selecting the item once the data has been loaded. There may be more to it to fit the exact behavior you want but it should be workable.
See the documentation here.

Notifydatasetchanged() between adapters

I have 2 recycleviews and both of them have a number of icons. I also have a list that contains a number, which is the number that represents which icon has a background that shows it has been clicked.
Now, the problem is that I only want one icon marked, with this I mean 1 item in total of those 2 recyclewviews. The way I've tried to do this is having a json that saves a number that determines which recycleview should have a marked item, if it doesn't have the number of the recycleview, then the background is the normal one, that way only one recycleview has a marked item.
The problem is that for that to work I need to call the method Notifydataserchanged() on the other adapter so the marked background dissappears, and the easiest way of doing that is putting the other adapter on the constructor. The problem with this is that only works for one, because since the other is declared before you can't construct it with the adapter that comes after it.
I've seen way of doing it with listeners/interfaces made in the activity and then
moved to the adapter itself so you can just call those methods, but it hasn't worked for me.
Any ideas? Thanks

How to make a ListView with draggable items?

I was looking for a ListPreference in which the user can change the order of items from a list. The items would be draggable and can be re-ordered by the user.
I saw this in my custom ROM (and I'm almost sure I saw it in Cyanogenmod) for the QuickPanel. Here's a screenshot to get the idea through:
I know how I can make custom ListView items and set the icon to indicate that the items are draggable, but I don't know how to make them draggable, and change the order accordingly. As for saving them in the preferences, I found this which could be implemented easily.
PS: I know Cyanogenmod is open-source, but I couldn't find the source for this particular thing :( The closest I could get was this, which should be somewhere near the other screen...
Thanks in advance for any tip about this.
UPDATE: I ended up using the files from the accepted answer, with additions and modifications. I am listing them here for further reference.
Use a custom Adapter (ArrayAdapter in my case), to implement the visual feedback that this item is draggable, which is an ImageView near the TextView. This is optional.
Set a DragListener and RemoveListener to update the list accordingly. The ListView does not do that automatically. And it depends on the Adapter you are using.
There was a line that casted a View to a ViewGroup, it made some errors, so I removed the cast without any issue, it was not needed. (in the onInterceptTouchEvent method).
Change mRemoveMode = 1; in the constructor of TouchInterceptor, or one of: FLING = 0; SLIDE = 1; TRASH = 2;. I think for TRASH, a resource should be available too.
I actually took the file not from the answer's link but from the Cyanogenmod one, which I already had, but I guess these files were the same.
These are the actual files in the project (at r12, at the time of writing):
The Preference using this ListView
The ListActivity with the listeners and the Adapter
The actual ListView
I hope it helps somebody else :)
There is no built-in widget to do this, but you may want take a look at the custom implementation used by the AOSP music player to allow for re-ordering of songs in playlists.
TouchInterceptor.java
That's the class which is extending ListView and is responsible for handling all of the MotionEvents and re-ordering its items, as well as detecting swipes for deleting items. You can see it implemented into an activity here in the TrackBrowserActivity.java class.
It has three interfaces you should also be aware of: DragListener, DropListener, and RemoveListener. You can use these interfaces to provide it callbacks to fire for those events, so that you can update changes made to the list order to your SavedPreferences (since the ListView will not handle that for you).
You can easily extend or modify the TouchInterceptor class to add additional functionality since the low-level stuff is all there for you.

Android: Is it possible to base a GridView on an adapter with filter capabaility?

BACKGROUND:
I have a GridView based on a fairly complex Adapter class which extends BaseAdapter. I find that I am doing much too much work in getView to calculate what to display and that often the number of items I want to display changes during those calculations. getView even gets called multiple times for the same position. It just seems like notifyDataSetChanged() gets called too soon.
This has caused much pain in my coding. I moved most of the code out into other routines, but find I still need to do some filtering of selections during the final display.
I've worked around this by setting the item.enabled(false), which works, but I'd rather eliminate the item all together.
I can't find a lifecycle for the GridView & underlying adapter, but I have determined that getCount() gets called a number of times before the items are displayed and while I can hook my changes in there, it just feels like I am having to work too hard here.
QUESTION:
Can I use a Filter class in connections with an Adapter tied to a GridView?
For example, my Adapter for the GridView has 36 items to display, and the Activity which contains it has a TextView set to value "3" which means only show every 3rd item. adapter.notifyDataSetChanged() gets called and immediately .getCount() wants to return 36, which really messes with getView
I know there are not a lot of specifics here, because I need to understand the concepts, not just find a solution for a specific problem.
There must be some functionality or way of looking at the problem that I am overlooking.
Have you overridden getCount? Why would it return 36 after the underlying data has changed?
If you make getCount depend on the underlying data I don't think you would have this problem:
#Override
public int getCount() {
return myArray.length; // or whatever your underlying data source is
}
Edit - Also, make sure you make changes to your data set before calling notifyDataSetChanged.

Android: Removing items from a ListView/ArrayAdapter Activity

I have a list (of messages) and I want to give the user the ability to remove these items from the list. I have extended an ArrayAdapter and give it an ArrayList of my messages and would like to simply remove an item from that list and then refresh the ListView instead of reloading the entire list of sent messages. The problem is, if there's only one message and I remove it using listAdapter.remove(messageObject), the adapter is still calling getView() and then throwing NullPointerExceptions all over the place. I'm not sure what the best way is to go about this.
Apparently things go a little haywire if you don't override the getCount() function in your custom ArrayAdapter. I set it to the size of my ArrayList and now everything seems to be working correctly.

Categories

Resources