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.
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? –
chuỗi con (0, 0)? –
để lấy chữ cái đầu tiên của từ –