2011-08-20 18 views
34

के पक्ष में वर्णमाला अक्षरों को कैसे दिखाया जाए I ListView को पक्ष में वर्णमाला अक्षरों (संपर्क सूची की तरह) बनाने के लिए कई ट्यूटोरियल देख चुके हैं, लेकिन वे सभी एक सूची एक्टिविटी क्लास का उपयोग करने लगते हैं और/या डेटाबेस से डेटा जबकि मैं सिर्फ एक सूची दृश्य (कोई विशेष गतिविधि) और डेटा के एक ArrayList का उपयोग कर रहा हूँ। क्या किसी को पता है कि मैं अपनी सूची सूची के लिए संपर्क सूची में उस वर्णमाला-स्क्रॉल सुविधा को कैसे कार्यान्वित कर सकता हूं?एंड्रॉइड लिस्ट व्यू

संपादित फिर

मैं this tutorial, जो मैंने सोचा था कि अंत में यह काम होगा पीछा किया, लेकिन मैं अभी भी एक करीबी के लिए मजबूर हो रही है।

class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer 
{ 
    private HashMap<String, Integer> alphaIndexer; 
    private String[] sections; 

    public AlphabeticalAdapter(Context c, int resource, List<String> data) 
    { 
     super(c, resource, data); 
     for (int i = 0; i < data.size(); i++) 
     { 
      String s = data.get(i).substring(0, 1).toUpperCase(); 
      alphaIndexer.put(s, i); 
     } 

     Set<String> sectionLetters = alphaIndexer.keySet(); 
     ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); 
     Collections.sort(sectionList); 
     sections = new String[sectionList.size()]; 
     sectionList.toArray(sections); 

    } 

    public int getPositionForSection(int section) 
    { 
     return alphaIndexer.get(sections[section]); 
    } 

    public int getSectionForPosition(int position) 
    { 
     return 1; 
    } 

    public Object[] getSections() 
    { 
     return sections; 
    } 
} 

LogCat में, यह java.lang.RuntimeException: Unable to resume activity {(package of another app I made) android.app.SuperNotCalledExcpetion कहते हैं। मैंने सोचा कि वास्तव में अजीब था क्योंकि मैं उस दूसरे ऐप का संदर्भ नहीं देता जिस पर मैं काम कर रहा हूं। मैंने उस ऐप को अनइंस्टॉल करने का प्रयास किया, फिर भी मजबूर बंद हो गया, लेकिन मैं देख सकता था कि लॉगकैट ने क्या कहा क्योंकि यह अपडेट नहीं हुआ था।

अंतिम संपादित

यहाँ काम कर कोड है। इसे 9 महीने के अतिदेय जैसे कुछ पोस्ट करने के लिए खेद है।

class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer 
{ 
    private HashMap<String, Integer> alphaIndexer; 
    private String[] sections; 

    public AlphabeticalAdapter(Context c, int resource, List<String> data) 
    { 
     super(c, resource, data); 
     alphaIndexer = new HashMap<String, Integer>(); 
     for (int i = 0; i < data.size(); i++) 
     { 
      String s = data.get(i).substring(0, 1).toUpperCase(); 
      if (!alphaIndexer.containsKey(s)) 
       alphaIndexer.put(s, i); 
     } 

     Set<String> sectionLetters = alphaIndexer.keySet(); 
     ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); 
     Collections.sort(sectionList); 
     sections = new String[sectionList.size()]; 
     for (int i = 0; i < sectionList.size(); i++) 
      sections[i] = sectionList.get(i); 
    } 

    public int getPositionForSection(int section) 
    { 
     return alphaIndexer.get(sections[section]); 
    } 

    public int getSectionForPosition(int position) 
    { 
     for (int i = sections.length - 1; i >= 0; i--) { 
      if (position >= alphaIndexer.get(sections[ i ])) { 
       return i; 
      } 
     } 
     return 0; 
    } 

    public Object[] getSections() 
    { 
     return sections; 
    } 
} 

संपादित करें: getSectionForPosition महत्वपूर्ण यदि आप अपने स्क्रोलर सही जगह में दिखाई देता है जबकि आप स्क्रॉल कर रहे हैं चाहते हैं। केवल 1 (या 0) लौटने से पता चलता है कि आप केवल पहले (या दूसरे) खंड में स्क्रॉल कर रहे हैं जिसके परिणामस्वरूप स्क्रोलर की गलत स्थिति होगी (ज्यादातर स्क्रीन से बाहर हो जाती है)। जोड़ा गया कोड उचित खंड देता है ताकि स्क्रॉलर यह जान सके कि यह वास्तव में कहां है।

+0

क्या कोई कारण है कि आप ListActivity का उपयोग नहीं करना चाहते हैं? क्या आप उन ट्यूटोरियल्स से लिंक कर सकते हैं जिनके बारे में आप बात करते हैं? –

+0

सबस्ट्रिंग (0, 0)? –

+0

शब्द –

उत्तर

21

मैं alphaIndexer का दृष्टांत करना भूल गया में एक महान उदाहरण है। अब पूरी तरह से काम करता है।

class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer 
{ 
    private HashMap<String, Integer> alphaIndexer; 
    private String[] sections; 

    public AlphabeticalAdapter(Context c, int resource, List<String> data) 
    { 
     super(c, resource, data); 
     alphaIndexer = new HashMap<String, Integer>(); 
     for (int i = 0; i < data.size(); i++) 
     { 
      String s = data.get(i).substring(0, 1).toUpperCase(); 
      if (!alphaIndexer.containsKey(s)) 
       alphaIndexer.put(s, i); 
     } 

     Set<String> sectionLetters = alphaIndexer.keySet(); 
     ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); 
     Collections.sort(sectionList); 
     sections = new String[sectionList.size()]; 
     for (int i = 0; i < sectionList.size(); i++) 
      sections[i] = sectionList.get(i); 
    } 

    public int getPositionForSection(int section) 
    { 
     return alphaIndexer.get(sections[section]); 
    } 

    public int getSectionForPosition(int position) 
    { 
     return 1; 
    } 

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

क्या आप अंतिम कार्यकारी कोड जोड़ सकते हैं? – akd

+0

@Pat: हैलो मैं अंतिम कामकाजी डेमो भी चाहता हूं यदि आपने इसे पूरा कर लिया है। धन्यवाद। –

+0

@akdurmus लंबे विलंब के लिए खेद है। –

3

यदि मैं आपको सही ढंग से समझता हूं, तो आप fastScrollEnabled=true सेट करना चाहते हैं और इससे आपको दाईं ओर थोड़ा अंगूठे स्क्रोलर मिल जाएगा। आप संपर्कों में से एक की तरह एक अस्थायी पत्र जोड़ना चाहते हैं, वहाँ List9.java (http://www.devdaily.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/src/com/example/android/apis/view/List9.java.shtml) में APIDemos परियोजना

+0

मुझे लगता है कि वे वास्तव में ए, बी, सी देखने के लिए पूछ रहे हैं हो जाएगा, आदि, आईफोन संपर्क सूची यूआई के समान। एक कस्टम स्क्रॉलबार विजेट प्रकार चीज। –

+0

स्क्रॉल के दौरान एक अनुभाग और एक दृश्य संवाद है। और FastScrollEnabled ListView पर सत्य पर सेट है। –

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