0

I have a Array list which contains Objects of Restaurants

ArrayList<Restaurants> array_sort = new ArrayList<Restaurants>();

Restaurant object is contain its name and Id. Beside of that there is a Alphabetical Index. I need to go for the specific letter in the array when user click on the index letter. This image will give you a good idea.

enter image description here

Thanks in advance !

3 Answers 3

3

Using an ArrayList for this task is not a good idea because it doesn't provide any efficient solution to your problem. I'd suggest to use a TreeMap<String,Resturant>. The good thing of the TreeMap is that it has the method

public NavigableMap<K,V> subMap(K fromKey,
                            boolean fromInclusive,
                            K toKey,
                            boolean toInclusive)

which returns a portion of the map that contains just the items you need for the specific letter, already sorted. This submap is backed by the full one so it's a light weight object.

You can easily do something like subMap("a", true, "b", false).

Sign up to request clarification or add additional context in comments.

Comments

0

You could enumerate the items in each of the letters before that letter and then use http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)

Comments

0

Easiest option can be to have a map also mapping display indices to list of restaurant objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.