2012-07-24 13 views
7

कहा जाता है मैंने ListView बनाया है जो FastScroll का उपयोग कर रहा है। (तस्वीर देखें) उपयोगकर्ता नीचे बटन के किसी भी क्लिक करता है (अर्थात। सभी ट्रैक, कलाकार, एल्बम), हर निम्न कस्टम ArrayAdaterएंड्रॉइड: फास्टस्क्रॉलिंग सेक्शन इंडेक्सर getSections() को केवल एक बार

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

कहा जाता है और एक ही ListView अद्यतन किया जाता है।

enter image description here

समस्या: एंड्रॉयड में मेरी जांच के अनुसार, getSections() विधि केवल एक बार कहा जाता है (अर्थात केवल जब पहले समय ScrollIndexListAdapter कहा जाता है)।

इस बार अनुभाग & पॉप्युलेट किए गए हैं, तेज़ स्क्रॉलिंग पूरी तरह से काम करता है।

लेकिन जब मैं कलाकार/एल्बम पर क्लिक करके ListView अद्यतन करता हूं, तो getSections() विधि नहीं कहा जाता है। इसलिए पुराने वर्गों का उपयोग किया जाता है, और फास्टस्क्रॉलिंग अभी भी पुराने अक्षरों के पूर्वावलोकन दिखाती है।

तो, सूची दृश्य को अद्यतन होने पर मैं हर बार अनुभाग कैसे अपडेट कर सकता हूं?

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

एंड्रॉइड पर मेरा अनुभव सीमित है, लेकिन मैं तर्क दूंगा कि अंतर्निहित डेटा स्रोत में परिवर्तन एडाप्टर में 'NotifyDataSetChanged() 'को कॉल करके प्रसारित किया जाना चाहिए। –

उत्तर

2

ऐसा लगता है कि नवीनतम FastScroll संस्करण है कि समस्या नहीं है। लेकिन, आपके मामले में, एक मोड़ है। सूची दृश्य में एडाप्टर सेट करते समय, तेजी से स्क्रॉलिंग को अक्षम और सक्षम करें। नीचे दिए गए कोड को देखें:

  adapter = new ScrollIndexListAdapter(this, data); 
      listView.setFastScrollEnabled(false); 
      listView.setAdapter(adapter); 
      listView.setFastScrollEnabled(true); 
+0

मुझे यह बग मिलती है और मेरे लिए यह ठीक काम करता है :) – JMR

संबंधित मुद्दे