2011-10-10 170 views
1

我試圖實現AlphabetIndexer來幫助用戶滾動瀏覽我的列表,但是當我運行應用程序時沒有任何東西顯示在列表中。有人能告訴我爲什麼嗎?無法使AlphabetIndexer工作

注意:我沒有在適配器的構造函數中實例化AlphabetIndexer,因爲在那時沒有Cursor可用。

下面是相關的代碼:

在活動的onCreate()方法:

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()是用於使用AsyncQueryHandler遊標查詢的方法。 AsyncQueryHandler如下所示:

private final class MyQueryHandler extends 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(); 
    } 
} 

} 

最後,我的SimpleCursorAdapter。我取出多餘的部分:

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(); 
} 

}

+0

我正在做類似的事情http://stackoverflow.com/questions/10224233/alphabetindexer-with-custom-adapter-managed-by-loadermanager – toobsco42

回答

3

有時Android將隱藏快速滾動功能,如果您的列表不是足夠長,以保證快速滾動。不知道這是否是您的問題,但可能值得嘗試添加一堆項目到列表中。

1

我剛剛在字母索引器和快速滾動上丟失了幾個小時。在我的情況下,列表並不總是足夠長的時間來禁止快速滾動/字母索引功能。確切的行爲可以在類FastScroller中找到,該類是AbsListView的幫助類。有一段代碼有沒有決定是否「列表很長」

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

MIN_PAGES與4.你有它,如果你的列表項數不至少4倍的兒童計(價值定義可見行)快速滾動,因此字母索引器不會出現。