How to Show Alphabetical Letters on Side of Android Listview

How to show alphabetical letters on side of Android ListView

I forgot to instantiate alphaIndexer. Works perfectly now.

class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer
{
private HashMap<String, Integer> alphaIndexer;
private String[] sections;

public AlphabeticalAdapter(Context c, int resource, List<String> data)
{
super(c, resource, data);
alphaIndexer = new HashMap<String, Integer>();
for (int i = 0; i < data.size(); i++)
{
String s = data.get(i).substring(0, 1).toUpperCase();
if (!alphaIndexer.containsKey(s))
alphaIndexer.put(s, i);
}

Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
for (int i = 0; i < sectionList.size(); i++)
sections[i] = sectionList.get(i);
}

public int getPositionForSection(int section)
{
return alphaIndexer.get(sections[section]);
}

public int getSectionForPosition(int position)
{
return 1;
}

public Object[] getSections()
{
return sections;
}
}

ListView with alphabets on the right, like the iPhone. Is it possible?

I implemented somthing similar a while back so i've modified my activity and you can take a look below, sorry its not very well commented - hope it helps!

  public class AZIndexer extends Activity {
ListView myListView;
ArrayList<String> elements;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// elements
String s = "MNBVCXZLKJHGFDSAQWERTYUIOP";
Random r = new Random();
elements = new ArrayList<String>();
for (int i = 0; i < 300; i++) {
elements.add(s.substring(r.nextInt(s.length())));
}
Collections.sort(elements); // Must be sorted!

// listview
myListView = (ListView) findViewById(R.id.myListView);
myListView.setFastScrollEnabled(true);
MyAZAdapter<String> adapter = new MyAZAdapter<String>(
getApplicationContext(), android.R.layout.simple_list_item_1,
elements);
myListView.setAdapter(adapter);

}

class MyAZAdapter<T> extends ArrayAdapter<T> implements SectionIndexer {
ArrayList<String> myElements;
HashMap<String, Integer> azIndexer;
String[] sections;

public MyAZAdapter(Context context, int textViewResourceId, List<T> objects) {
super(context, textViewResourceId, objects);
myElements = (ArrayList<String>) objects;
azIndexer = new HashMap<String, Integer>(); //stores the positions for the start of each letter

int size = elements.size();
for (int i = size - 1; i >= 0; i--) {
String element = elements.get(i);
//We store the first letter of the word, and its index.
azIndexer.put(element.substring(0, 1), i);
}

Set<String> keys = azIndexer.keySet(); // set of letters

Iterator<String> it = keys.iterator();
ArrayList<String> keyList = new ArrayList<String>();

while (it.hasNext()) {
String key = it.next();
keyList.add(key);
}
Collections.sort(keyList);//sort the keylist
sections = new String[keyList.size()]; // simple conversion to array
keyList.toArray(sections);
}

public int getPositionForSection(int section) {
String letter = sections[section];
return azIndexer.get(letter);
}

public int getSectionForPosition(int position) {
Log.v("getSectionForPosition", "called");
return 0;
}

public Object[] getSections() {
return sections; // to string will be called to display the letter
}
}
}

With xml as:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>

ScreenShot:

Sample Image

Android Listview like iphone contacts list(alphabets in right side of the list)

There is nothing like that in android you have to create custom view. Try from iphone-uitable-view-in-android
and sideindex-for-android. I have used code from these two links to create iphone like list with alphabets on side.

Listview in alphabetical order

you can use ArrayAdapter.sort

friendsListAdapter.sort(new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs);
}
});

How to create an alphabetical scrollbar displaying all the letter in android?

This is an iPhone feature, Android uses fast scroll. I'd recommend that you use the platform alternative rather than try to enforce common functionality.

If you must, you'll have to implement this yourself. Put your list view in a RelativeLayout and put A-Z TextViews in a vertical LinearLayout that is set to layout_alignParentRight="true". Set the TextView's tag to A-Z appropiately and set onClick="quickScroll" on all of them.

Implement in your Activity:

public void quickScroll(View v) {
String alphabet = (String)v.getTag();
//find the index of the separator row view
list.setSelectionFromTop(index, 0);
}

This will scroll to the selected letter onClick, but I believe you can scroll your finger over the alphabet on iPhone and it'll update the list? You'll have to implement an onTouchListener rather than onClickListener for that.



Related Topics



Leave a reply



Submit