gpt4 book ai didi

android - 如何在 ListView 中实现字母索引器?

转载 作者:行者123 更新时间:2023-11-29 01:34:51 24 4
gpt4 key购买 nike

我想在包含字母的 Android 音乐播放器中实现 ScrollView 。它应该显示从被点击的相同字母表开始的歌曲

最佳答案

尝试这样的事情 - 将自定义 ArrayAdapter 分配给列表,自定义 ArrayAdapter 将实现 SectionIndexer。

首先,使用 ListView 创建一个 xml 布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">

<ListView
android:id="@+id/thelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
</LinearLayout>

其次,创建一个对象来保存您的项目(具有您喜欢的所有属性):

public class Holder {

public int id;
public String name;
public String direction;
public String address;
public String phone;
}

最后,使用实现SectionIndexer

的自定义适配器创建您的 Activity
/**
* The Holder List Activity
*/
public class HolderListActivity extends Activity {
private DBAdapter db;

private LinkedList<Holder> holderList = new LinkedList<Holder>();
private HolderListAdaptor holderListAdaptor;

private ListView list;

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.storelist);

list = (ListView) findViewById(R.id.thelist);

// populate the store list
holderList = db.getAllStoresOrderByName(holderList);

// create an adaptor with the holder list
holderListAdaptor = new HolderListAdaptor(this,holderList);

// assign the listview an adaptor
list.setAdapter(holderListAdaptor);

}

/**
* The List row creator
*/
class HolderListAdaptor extends ArrayAdapter<Holder> implements SectionIndexer{

HashMap<String, Integer> alphaIndexer;
String[] sections;

public HolderListAdaptor(Context context, LinkedList<Holder> items) {
super(context, R.layout.storerow, items);

alphaIndexer = new HashMap<String, Integer>();
int size = items.size();

for (int x = 0; x < size; x++) {
Store s = items.get(x);

// get the first letter of the store
String ch = s.name.substring(0, 1);
// convert to uppercase otherwise lowercase a -z will be sorted after upper A-Z
ch = ch.toUpperCase();

// HashMap will prevent duplicates
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);
}

public View getView(int position, View convertView, ViewGroup parent) {
// expand your row xml if you are using custom xml per row
}

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

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

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

一切顺利!

关于android - 如何在 ListView 中实现字母索引器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28963397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com