2011-06-29 117 views
0

中按字母順序的標題部分我需要以數字形式顯示ListView項目,然後按字母順序標題部分。但我的要求是首先它應該顯示帶有數字標題部分的ListView項目,然後應該在按字母順序標題部分顯示ListView項目之後。但是我得到了如下的混合結果。如何顯示帶有數字的ListView項目,然後在android

A,1,B,2,C,3(標題區)

但是我的要求如下

1,2,3,A,B,C (以及標題部分下方的listView項)

+0

你見過[ExpandableListView](http://developer.android.com/reference/android/widget/ExpandableListView.html) –

回答

0

您需要您的適配器以您想要的任何順序返回列表。 getView方法應根據位置參數的值返回適當的項目。

1

您可以使用我的Simple Section Adapter來做到這一點。只需按升序對您的列表進行排序,從1 ... N,A-Z開始,並使用以下代碼創建Sectionizer

public class NumericAndAlphabetSectionizer implements Sectionizer<YourObjectType> { 

    @Override 
    public String getSectionTitleForItem(YourObjectType instance) { 
     return instance.getName().toUpperCase().substring(0, 1); 
    } 
} 

創建分區後,只需將自定義適配器包裝好即可。

sectionAdapter = new SimpleSectionAdapter<YourObjectType>(context, 
      yourAdapter, R.layout.section_header, R.id.title, 
      new NumericAndAlphabetSectionizer()); 

就是這樣。你完成了。

相關問題