Android字母索引器英语和希伯来语(Unicode)合用
下面是我使用AlphaoIndexer的listview适配器类。 当我将unicode字符(希伯来语)和英语一起添加时,它不起作用。 我在:getSectionForPosition中遇到异常。进入索引-1。。。。 使用DB-1中的两个条目进行尝试,其中一个条目以希伯来文字符(unicode)开头,另一个条目以英语开头。字母索引器的第一个字符是unicode字符。 在这件事上真的需要帮助Android字母索引器英语和希伯来语(Unicode)合用,android,listview,unicode,indexer,Android,Listview,Unicode,Indexer,下面是我使用AlphaoIndexer的listview适配器类。 当我将unicode字符(希伯来语)和英语一起添加时,它不起作用。 我在:getSectionForPosition中遇到异常。进入索引-1。。。。 使用DB-1中的两个条目进行尝试,其中一个条目以希伯来文字符(unicode)开头,另一个条目以英语开头。字母索引器的第一个字符是unicode字符。 在这件事上真的需要帮助 public abstract class RecipeListViewAdapter extends S
public abstract class RecipeListViewAdapter extends SimpleCursorAdapter implements SectionIndexer
{
protected Context mContext;
protected Cursor mCursor;
private LayoutInflater mInflater;
public static final int TYPE_HEADER = 1;
public static final int TYPE_NORMAL = 0;
private static final int TYPE_COUNT = 2;
private AlphabetIndexer indexer;
private int[] usedSectionNumbers;
private Map<Integer, Integer> sectionToOffset;
private Map<Integer, Integer> sectionToPosition;
protected ImageLoader mImageLoader = new ImageLoader( MyApp.Instance() );
public RecipeListViewAdapter( Context context,
int layout,
Cursor c,
String[] from,
int[] to )
{
super(context, layout, c, from, to);
mContext = context;
mCursor = c;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
indexer = new AlphabetIndexer(c, c.getColumnIndexOrThrow("NAME"), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
sectionToPosition = new TreeMap<Integer, Integer>();
sectionToOffset = new HashMap<Integer, Integer>();
final int count = super.getCount();
int i;
for( i = count - 1 ; i >= 0; i-- )
{
sectionToPosition.put(indexer.getSectionForPosition(i), i);
}
i = 0;
usedSectionNumbers = new int[sectionToPosition.keySet().size()];
for( Integer section : sectionToPosition.keySet() )
{
sectionToOffset.put(section, i);
usedSectionNumbers[i] = section;
i++;
}
for(Integer section: sectionToPosition.keySet())
{
sectionToPosition.put(section, sectionToPosition.get(section) + sectionToOffset.get(section));
}
}
@Override
public int getCount()
{
if( super.getCount() != 0 )
{
return super.getCount() + usedSectionNumbers.length;
}
return 0;
}
@Override
public Object getItem(int position)
{
if (getItemViewType(position) == TYPE_NORMAL) //we define this function later
{
return super.getItem( GetItemPosition( position ) );
}
return null;
}
public int GetItemPosition( final int position )
{
return position - sectionToOffset.get(getSectionForPosition(position)) - 1;
}
public int getPositionForSection(int section) {
if (! sectionToOffset.containsKey(section)){
int i = 0;
int maxLength = usedSectionNumbers.length;
while (i < maxLength && section > usedSectionNumbers[i]){
i++;
}
if (i == maxLength) return getCount();
return indexer.getPositionForSection(usedSectionNumbers[i]) + sectionToOffset.get(usedSectionNumbers[i]);
}
return indexer.getPositionForSection(section) + sectionToOffset.get(section);
}
public int getSectionForPosition(int position) {
int i = 0;
int maxLength = usedSectionNumbers.length;
while (i < maxLength && position >= sectionToPosition.get(usedSectionNumbers[i])){
i++;
}
return usedSectionNumbers[i-1];
}
public Object[] getSections() {
return indexer.getSections();
}
//nothing much to this: headers have positions that the sectionIndexer manages.
@Override
public int getItemViewType(int position) {
if (position == getPositionForSection(getSectionForPosition(position))){
return TYPE_HEADER;
} return TYPE_NORMAL;
}
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
@Override
public View getView( int position,
View convertView,
ViewGroup parent )
{
final int type = getItemViewType(position);
if (type == TYPE_HEADER)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.header, parent, false);
}
((TextView)convertView.findViewById(R.id.header)).setText((String)getSections()[getSectionForPosition(position)]);
return convertView;
}
else
{
ViewHolder holder = new ViewHolder();
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.recipes_list_view_entry, null);
holder.name = (TextView) convertView.findViewById( R.id.name_entry );
holder.author = (TextView) convertView.findViewById( R.id.username_entry );
holder.ratingBar = (RatingBar) convertView.findViewById( R.id.list_RatingBarId );
holder.userRatingBar = (RatingBar) convertView.findViewById( R.id.list_userRatingBarId );
holder.diffculty = (ImageView) convertView.findViewById( R.id.list_DifficultyImageViewId );
holder.preparationTime = (ImageView) convertView.findViewById( R.id.list_TimeImageViewId );
holder.recipePic = new DisplayableImageView( (ImageView) convertView.findViewById( R.id.list_RecipeImageViewId ) );
holder.name.setTypeface( MyApp.Fonts.ARIAL );
holder.name.setTextSize( MyApp.Fonts.RUNNING_TEXT_SIZE );
holder.name.setTextColor( Color.BLACK );
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if( super.getItem( GetItemPosition(position) ) != null )
{
// Check if single
if( getCount() == position+1 && position == 1 )
{
convertView.setBackgroundResource( R.drawable.list_single );
}
else if( getItemViewType(position-1) == TYPE_HEADER )
{
// Check if single item in the middle of the list
if( getItemViewType(position+1) == TYPE_HEADER )
{
convertView.setBackgroundResource( R.drawable.list_single );
}
else if( position == getCount() - 1 )
{
convertView.setBackgroundResource( R.drawable.list_single );
}
else
{
convertView.setBackgroundResource( R.drawable.list_up );
}
}
else
{
// Middle or bottom
convertView.setBackgroundResource( R.drawable.list_middle );
//If not last
if( getCount() != position + 1 )
{
// Check if middle or down
if( getItemViewType(position+1) == TYPE_HEADER )
{
convertView.setBackgroundResource( R.drawable.list_down );
}
else
{
convertView.setBackgroundResource( R.drawable.list_middle );
}
}
else
{
// If it is last - use list_down
convertView.setBackgroundResource( R.drawable.list_down );
}
}
FillRecipeDataToHolder( GetItemPosition(position), holder );
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
}
//these two methods just disable the headers
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
if (getItemViewType(position) == TYPE_HEADER){
return false;
}
return true;
}
protected abstract void FillRecipeDataToHolder(int position, ViewHolder holder);
static class ViewHolder
{
TextView separator;
DisplayableImageView recipePic;
TextView name;
TextView author;
RatingBar ratingBar;
RatingBar userRatingBar;
ImageView diffculty;
ImageView preparationTime;
TextView serveCount;
}
}
公共抽象类RecipeListViewAdapter扩展了SimpleCorsAdapter实现了SectionIndexer
{
保护上下文mContext;
保护光标mCursor;
私人停车场;
公共静态最终整数类型_头=1;
公共静态最终整数类型_NORMAL=0;
私有静态最终整数类型_计数=2;
专用字母索引器;
私有int[]使用的分区编号;
私人地图部分设置为偏移;
私密地图;
受保护的ImageLoader mImageLoader=新的ImageLoader(MyApp.Instance());
公共RecipeListViewAdapter(上下文,
int布局,
光标c,
字符串[]来自,
int[]至)
{
super(上下文、布局、c、from、to);
mContext=上下文;
mCursor=c;
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
索引器=新的字母索引器(c,c.getColumnIndexOrThrow(“名称”),“ABCDEFGHIJKLMNOPQRSTUVWXYZ”);
sectionToPosition=新树映射();
sectionToOffset=新HashMap();
final int count=super.getCount();
int i;
对于(i=count-1;i>=0;i--)
{
sectionToPosition.put(索引器getSectionForPosition(i),i);
}
i=0;
usedSectionNumbers=new int[sectionToPosition.keySet().size()];
对于(整数部分:sectionToPosition.keySet())
{
第Tooffset.put节(第一节);
UsedSectionNumber[i]=区段;
i++;
}
对于(整数部分:sectionToPosition.keySet())
{
sectionToPosition.put(section,sectionToPosition.get(section)+sectionToOffset.get(section));
}
}
@凌驾
public int getCount()
{
如果(super.getCount()!=0)
{
返回super.getCount()+usedSectionNumber.length;
}
返回0;
}
@凌驾
公共对象getItem(int位置)
{
if(getItemViewType(position)==TYPE\u NORMAL)//我们稍后定义此函数
{
返回super.getItem(GetItemPosition(position));
}
返回null;
}
公共整数GetItemPosition(最终整数位置)
{
返回位置-sectionToOffset.get(getSectionForPosition(position))-1;
}
公共int getPositionForSection(int段){
如果(!sectionToOffset.containsKey(节)){
int i=0;
int maxLength=UsedSectionNumber.length;
而(iUsedSectionNumber[i]){
i++;
}
如果(i==maxLength)返回getCount();
返回indexer.getPositionForSection(UsedSectionNumber[i])+sectionToOffset.get(UsedSectionNumber[i]);
}
返回索引器.getPositionForSection(section)+sectionToOffset.get(section);
}
公共int getSectionForPosition(int位置){
int i=0;
int maxLength=UsedSectionNumber.length;
而(i=sectionToPosition.get(UsedSectionNumber[i])){
i++;
}
返回使用的分区编号[i-1];
}
公共对象[]getSections(){
返回indexer.getSections();
}
//这没什么大不了的:标题有sectionIndexer管理的位置。
@凌驾
public int getItemViewType(int位置){
如果(位置==getPositionForSection(getSectionForPosition(位置))){
返回类型_头;
}返回类型_正常;
}
@凌驾
public int getViewTypeCount(){
返回类型\u计数;
}
@凌驾
公共视图getView(内部位置,
视图转换视图,
视图组(父级)
{
最终int类型=getItemViewType(位置);
if(type==type\u头)
{
if(convertView==null)
{
convertView=mInflater.充气(R.layout.header,父项,false);
}
((TextView)convertView.findViewById(R.id.header)).setText((字符串)getSections()[getSectionForPosition(position)];
返回视图;
}
其他的
{
ViewHolder=新的ViewHolder();
if(convertView==null)
{
convertView=mInflater.充气(R.layout.recipes\u list\u view\u条目,空);
holder.name=(TextView)convertView.findViewById(R.id.name\u条目);
holder.author=(TextView)convertView.findViewById(R.id.username\u条目);
holder.ratingBar=(ratingBar)convertView.findViewById(R.id.list\U RATINGBAID);
holder.userRatingBar=(RatingBar)convertView.findViewById(R.id.list\u userRatingBarId);
holder.diffculty=(ImageView)convertView.findViewById(R.id.list\u diffcultyImageViewId);
holder.preparationTime=(ImageView)convertView.findViewById(R.id.list\u TimeImageViewId);
holder.recipePic=新的DisplayableImageView((ImageView)convertView.findViewB