实现 SectionIndexer 类代码如下:
- Java code
package com.zhang.crm;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import com.zhang.R;import android.content.Context;import android.database.Cursor;import android.graphics.drawable.Drawable;import android.view.View;import android.widget.AlphabetIndexer;import android.widget.ImageView;import android.widget.SectionIndexer;import android.widget.SimpleCursorAdapter;public class testSimpleCursorAdapter extends SimpleCursorAdapter implements SectionIndexer{ private Cursor _cursor; private Context _context; AlphabetIndexer alphabetIndexer; public testSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); _cursor = c; _context= context; alphabetIndexer = new AlphabetIndexer(_cursor, _cursor.getColumnIndexOrThrow(SystemConst.TABLE_FIELD_CLIENTNAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ"); } @Override public void bindView(View view, Context context, Cursor cursor) { ImageView imageView = (ImageView)view.findViewById(R.id.image); byte aa[] = new byte[1024*20]; int ch; InputStream input = context.getResources().openRawResource(R.raw.listimage); try { do { ch = input.read(aa); } while (ch!=-1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ByteArrayInputStream stream = new ByteArrayInputStream(aa); imageView.setImageDrawable(Drawable.createFromStream(stream, "img")); super.bindView(view, context, cursor); } @Override public int getPositionForSection(int section) { return alphabetIndexer.getPositionForSection(section); } @Override public int getSectionForPosition(int position) {// return alphabetIndexer.getSectionForPosition(position); return 0; } @Override public Object[] getSections() { if (alphabetIndexer != null) { return alphabetIndexer.getSections(); } else { return null; } } }
接着是这么调用的:
- Java code
cursor = getContentResolver().query(SystemConst.CONTENT_URI,null, null, null, null); startManagingCursor(cursor);mIndexer = new CrmAlphabetIndexer(cursor, cursor.getColumnIndexOrThrow(SystemConst.TABLE_FIELD_CLIENTNAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ"); scAdapter = new testSimpleCursorAdapter(getApplicationContext(), R.layout.crmlist, cursor, new String[]{SystemConst.TABLE_FIELD_CLIENTNAME,SystemConst.TABLE_FIELD_LINKMAN,SystemConst.TABLE_FIELD_TELPHONE},new int[]{R.id.clientName,R.id.linkName,R.id.telphone}); clientlist.setAdapter(scAdapter);
可是显示的结果没有按照汉字的首字母进行排序,求高手指教 。。。。小弟感谢了
------解决方案--------------------
你有做排序算法吗
------解决方案--------------------
我做过一个是先取得拼音后,通过比较器给他们进行排序
------解决方案--------------------
网上有很多生成拼音的教程,你搜一下 比较器也很简单搜一下就有,取得拼音后用比较器比较第一个字节可对list进行排序