开发者

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

I am new to android development. I have been searching for list view similar to c开发者_运维问答ontacts list i.e a list with alphabetic indexing panel on the right side.

Thanks.


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.


The code in sideindex-for-android is more complex than it needs to be.

I created the LinearLayout as that example did and added TextView instances for the letters. But then I had them clickable.

In my onClick(), I see if it is one of these views and get the text from it.

When I loaded my list, I set up a dictionary in the cursoradapter. The setSelection() method gets the offset from this dictionary.

public static final String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public Map<String, Integer> getAlphabetOffsets() {

    HashMap<String, Integer> map = new HashMap<>();

    // First initialize the dictionary with all of the letters of the alphabet.
    //
    for (int idx = 0; idx < alphabet.length(); idx++) {
        String aLetter = alphabet.substring(idx, idx+1);
        map.put(aLetter, -1);
    }

    int numFound = cursor.getCount();

    cursor.moveToFirst();

    // Now go through the products' first initials and, when an initial has not been
    // found, set the offset for that letter.
    //
    for (int idx = 0; idx < numFound; idx++) {

        String productName = cursor.getString(cursor.getColumnIndex(DB.PRODUCTS_NAME_COL));

        String current;

        if (productName == null || productName.equals("")) {
            current = "0";
        } else {
            current = productName.substring(0, 1).toUpperCase();
        }

        // By the way, what do we do if a product name does not start with a letter or number?
        //
        // For now, we will ignore it. We are only putting 0-9 and A-Z into the side index.
        //
        if (map.containsKey(current) && map.get(current) < 0)
            map.put(current, idx);

        cursor.moveToNext();
    }

    map.put("0", 0);

    int lastFound = 0;

    /*
    Now we deal with letters in the alphabet for which there are no products.

    We go through the alphabet again. If we do not have an offset for a letter,
    we use the offset for the previous letter.

    For example, say that we do not have products that start with "B" or "D", we
    might see:

            { "9" = 0, "A" = 1, "B" = -1, "C" = 5, "D" = -1, "E" = 10 }

    After this runs, will we have:

            { "9" = 0, "A" = 1, "B" = 1, "C" = 5, "D" = 5, "E" = 10 }

    This is so if we click on B, we see the list starting a "A" and see that
    there are no "B" products.

    */
    for (int idx = 0; idx < alphabet.length(); idx++) {

        String current = alphabet.substring(idx, idx+1);

        if ( map.get(current) < 0 ) {
            map.put(current, lastFound);
        } else {
            lastFound = map.get(current);
        }
        System.out.println("alphabet \"" + current + "\" = " + map.get(current));
    }

    return map;
}


there is nothing like that in android (except samsung's contacts view)

the default in android is using listview with fast scroll like the native contact list

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜