实现 SectionIndexer 类代码如下:
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;
}
}
}
接着是这么调用的:
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);
可是显示的结果没有按照汉字的首字母进行排序,求高手指教 。小弟感谢了
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;
}
}
}
接着是这么调用的:
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);
可是显示的结果没有按照汉字的首字母进行排序,求高手指教 。小弟感谢了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货