2011-08-20 67 views
34

Tôi đã xem rất nhiều hướng dẫn để tạo ListView có các chữ cái ở bên cạnh (như danh sách Liên hệ), nhưng tất cả dường như sử dụng lớp ListActivity và/hoặc dữ liệu từ cơ sở dữ liệu trong khi tôi chỉ sử dụng một ListView (không có hoạt động đặc biệt) và một ArrayList dữ liệu. Có ai biết làm thế nào tôi có thể thực hiện tính năng cuộn chữ cái trong danh sách liên lạc cho ListView của riêng tôi?Làm thế nào để hiển thị các chữ cái ở bên cạnh Android ListView

Edited Again

Tôi đi theo this tutorial, mà tôi nghĩ rằng cuối cùng sẽ làm cho nó hoạt động, nhưng tôi vẫn nhận được một buộc chặt chẽ.

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

Trong LogCat, mã số java.lang.RuntimeException: Unable to resume activity {(package of another app I made) android.app.SuperNotCalledExcpetion. Tôi nghĩ điều đó thực sự kỳ lạ vì tôi không tham chiếu đến ứng dụng khác đó trong ứng dụng tôi đang làm việc. Tôi đã thử gỡ cài đặt ứng dụng khác, vẫn bị buộc phải đóng, nhưng tôi có thể thấy LogCat đã nói gì vì nó không cập nhật.

cuối cùng Sửa

Dưới đây là đoạn code làm việc. Xin lỗi vì đã đăng nó như 9 tháng quá hạn.

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

Chỉnh sửa: getSectionForPosition quan trọng nếu bạn muốn cuộn giấy xuất hiện ở đúng vị trí trong khi cuộn. Quay trở lại chỉ 1 (hoặc 0) cho biết bạn chỉ đang cuộn trong phần đầu tiên (hoặc thứ hai) mà sẽ dẫn đến vị trí sai của scroller (hầu hết là hết màn hình). Mã được thêm trả về phần thích hợp để cuộn có thể biết nó thực sự ở đâu.

+0

Có lý do nào bạn không muốn sử dụng ListActivity không? Bạn có thể liên kết đến các hướng dẫn bạn nói không? –

+0

chuỗi con (0, 0)? –

+0

để lấy chữ cái đầu tiên của từ –

Trả lời

21

Tôi quên khởi tạo alphaIndexer. Hoạt động hoàn hảo ngay bây giờ.

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

Bạn có thể thêm mã làm việc cuối cùng không? – akd

+0

@Pat: Xin chào tôi cũng muốn bản demo làm việc cuối cùng nếu bạn đã hoàn thành nó. Cảm ơn. –

+0

@akdurmus xin lỗi vì sự chậm trễ lâu dài. –

3

Nếu tôi hiểu bạn một cách chính xác, bạn muốn đặt fastScrollEnabled=true và điều đó sẽ cung cấp cho bạn nút gạt ngón tay cái nhỏ ở bên phải. Nếu bạn muốn thêm một chữ cái nổi như chữ cái trong danh bạ, có một ví dụ tuyệt vời trong dự án APIDemos trong List9.java (http://www.devdaily.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/src/com/example/android/apis/view/List9.java.shtml)

+0

Tôi nghĩ rằng họ đang yêu cầu thực sự nhìn thấy A, B, C, vv, tương tự như giao diện người dùng danh sách liên hệ iPhone. Một tùy chọn loại thanh cuộn tùy chỉnh. –

+0

Có một phần và hộp thoại hiển thị trong khi cuộn. Và fastScrollEnabled được đặt thành true trên ListView. –

Các vấn đề liên quan