开发者

Unable to make AlphabetIndexer work

开发者 https://www.devze.com 2023-04-11 20:31 出处:网络
I\'m attempting to implement the AlphabetIndexer to help the users scroll through my list, but nothing shows up on the list when I run the app.Could someone please tell me why?

I'm attempting to implement the AlphabetIndexer to help the users scroll through my list, but nothing shows up on the list when I run the app. Could someone please tell me why?

Note: I am not instantiating an AlphabetIndexer in the Adapter's constructor because, at that point, no Cursor is available.

Here is the relevant code:

In the Activity's onCreate() method:

mList = (ListView)findViewById(R.id.mylist);
mList.setOnItemClickListener(this);
mList.setFastScrollEnabled(true);
mAdapter = new MyAdapter(MyActivity.this, R.layout.layout_list_row, null, new String[] {MyColumns.NAME}, new int[] {R.id.itemname});
mList.setAdapter(mAdapter);
mList.setFastScrollEnabled(true);
doQuery();

doQuery() is a method that queries for a Cursor using an AsyncQueryHandler. The AsyncQueryHandler looks like this:

private final class MyQueryHandler extend开发者_如何学编程s AsyncQueryHandler {
    public MyQueryHandler(Context context) {
        super(context.getContentResolver());
    }
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
    if (!isFinishing()) {
        if (mAdapter != null) {
            mAdapter.changeCursor(cursor);
        }
    }
    else {
        cursor.close();
    }
}

}

Lastly, my SimpleCursorAdapter. I've taken out the unnecessary parts:

public class MyAdapter extends SimpleCursorAdapter implements View.OnClickListener {

    private Cursor mCursor;
    AlphabetIndexer alphaIndexer;


public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
}

public int getPositionForSection(int section) {
    return alphaIndexer.getPositionForSection(section);
}

public int getSectionForPosition(int position) {
    return alphaIndexer.getSectionForPosition(position);
}

public Object[] getSections() {
    return alphaIndexer.getSections();
}

public void onClick(View v) {
    // ...
}       

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // ...
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // ...
}

@Override
public void changeCursor(Cursor cursor) {
    super.changeCursor(cursor);

    if (MyActivity.this.mCursor != null) {
        stopManagingCursor(MyActivity.this.mCursor);
        MyActivity.this.mCursor.close();
        MyActivity.this.mCursor = null;
        mCursor = null;
    }
    MyActivity.this.mCursor = cursor;
    startManagingCursor(MyActivity.this.mCursor);
    mCursor = cursor;
    alphaIndexer = new AlphabetIndexer(mCursor, mCursor.getColumnIndex(MyColumns.NAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    alphaIndexer.setCursor(mCursor);
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    return doQuery();
}

}


Sometimes Android will hide the fast-scroll functionality if your list isn't long enough to warrant fast-scrolling. Not sure if that's your problem, but it might be worth trying to add a bunch of items to the list.


I've just lost couple of hours on alphabet indexer and fast scroller. In my case the list wasn't always long enough to warant the fast scroll/alphabet indexer feature. The exact behavior can be found in class FastScroller which is a helper class for AbsListView. There is a piece of code there that decides if "the list is long"

final boolean longList = childCount > 0 && itemCount / childCount >= MIN_PAGES;

MIN_PAGES is defined with value of 4. There you have it, if your list item count is not at least 4x the child count (visible rows) fast scroller and thus alphabet indexer will not appear.

0

精彩评论

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