2012-07-24 23 views
7

FastScrollを使用してListViewを作成しました。ユーザーは毎回次のカスタムArrayAdaterがAndroid:FastScrolling SectionIndexer getSections()は一度だけ呼び出されます

ArrayAdapter<String> adapter = new ScrollIndexListAdapter(Listing.this, elements); 
//Code for ScrollIndexListAdapter is below 

と呼ばれ、同じリストビューが更新される。、下のボタン(すなわち。すべてのトラック、アーティスト、アルバム)のいずれかをクリックすると(写真を参照)

enter image description here

問題:アンドロイドで私の調査によると、getSections()方法は一度だけ呼び出される(すなわち最初時間ScrollIndexListAdapterが呼び出されたときのみ)。

このセクションでは、&のセクションにはfastScrollingが完全に機能します。

しかし、アーティスト/アルバムをクリックしてListViewを更新すると、getSections()メソッドは呼び出されません。したがって、古いセクションが使用され、FastScrollingは古いアルファベットのプレビューを表示します。

ListViewが更新されるたびにセクションが更新されるようにするにはどうすればよいですか?

setSections()メソッドがありますが、使用方法がわかりません。 ScrollIndexListAdapterクラスの

コード:

public class ScrollIndexListAdapter extends ArrayAdapter implements 
     SectionIndexer { 

    // Variables for SectionIndexer List Fast Scrolling 
    HashMap<String, Integer> alphaIndexer; 
    String[] sections; 
    private static ArrayList<String> list = new ArrayList<String>(); 

    public ScrollIndexListAdapter(Context context, ArrayList<String> list) { 
     super(context, android.R.layout.simple_list_item_1, android.R.id.text1, 
       list); 
     this.list.clear(); 
     this.list.addAll(list); 
     /* 
     * Setting SectionIndexer 
     */ 
     alphaIndexer = new HashMap<String, Integer>(); 
     int size = list.size(); 
     for (int x = 0; x < size; x++) { 
      String s = (String) list.get(x); 
      // Get the first character of the track 
      String ch = s.substring(0, 1); 
      // convert to uppercase otherwise lowercase a -z will be sorted 
      // after upper A-Z 
      ch = ch.toUpperCase(); 
      if (!alphaIndexer.containsKey(ch)) { 
       alphaIndexer.put(ch, x); 
      } 
     } 
     Set<String> sectionLetters = alphaIndexer.keySet(); 
     // create a list from the set to sort 
     ArrayList<String> sectionList = new ArrayList<String>(
       sectionLetters); 
     Collections.sort(sectionList); 
     sections = new String[sectionList.size()]; 
     sectionList.toArray(sections); 
    } 

    /* 
    * Methods for AphhabelIndexer for List Fast Scrolling 
    */ 
    @Override 
    public int getPositionForSection(int section) { 
     String letter = (String) sections[section]; 
     return alphaIndexer.get(letter); 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     String letter = (String) sections[position]; 
     return alphaIndexer.get(letter); 
    } 

    @Override 
    public Object[] getSections() { 
     return sections; 
    } 
} 
+0

私のAndroidでの経験は限られていますが、アダプタの 'notifyDataSetChanged()'を呼び出すことで、基礎となるデータソースの変更をブロードキャストする必要があると私は主張します。 –

答えて

2

最新FastScrollバージョンはその問題を持っていないようです。しかし、あなたの場合、ターンアラウンドがあります。アダプタをListViewに設定するときは、高速スクロールを無効にして有効にします。

+0

私はこのバグを見つけ、これはうまく動作します:) – JMR

関連する問題