Android 如何在自定义ArrayAdapter上使用SectionIndexer?

Android 如何在自定义ArrayAdapter上使用SectionIndexer?,android,android-arrayadapter,sectionindexer,Android,Android Arrayadapter,Sectionindexer,我正在尝试实现将我的所有产品从数据库中获取到listview。为此,我使用了定制的arrayadapter&同时我想在listview上使用SectionIndexer。我已经到了这一步。这是我的密码 public class MySimpleArrayAdapter extends ArrayAdapter<ListClass> implements SectionIndexer { private final Context context; private final Ar

我正在尝试实现将我的所有产品从数据库中获取到listview。为此,我使用了定制的arrayadapter&同时我想在listview上使用SectionIndexer。我已经到了这一步。这是我的密码

public class MySimpleArrayAdapter extends ArrayAdapter<ListClass>  implements SectionIndexer {

private final Context context;
private final ArrayList<ListClass> values;
AlphabetIndexer index;

public MySimpleArrayAdapter(Context context, Cursor cursor, ArrayList<ListClass> values) {
    super(context, android.R.layout.simple_list_item_1, values);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.values = values;
    index = new AlphabetIndexer(cursor, cursor.getColumnIndex(Constants.PRODUCT_NAME), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.single_item, parent, false);
    ListClass lc = values.get(position);
    TextView textView1 = (TextView) rowView.findViewById(R.id.textView1);
    TextView textView2 = (TextView) rowView.findViewById(R.id.textView2);
    TextView textView3 = (TextView) rowView.findViewById(R.id.textView3);
    TextView textView4 = (TextView) rowView.findViewById(R.id.textView4);
    TextView textView5 = (TextView) rowView.findViewById(R.id.textView5);
    TextView textView6 = (TextView) rowView.findViewById(R.id.textView6);
    textView1.setText(lc.Product);
    textView2.setText(lc.MRP);
    textView3.setText(lc.Qty);
    textView4.setText(lc.PRO);
    textView5.setText(lc.SCH);
    textView6.setText(lc.ACT);
    return rowView;
}

@Override
public int getPositionForSection(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getSectionForPosition(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object[] getSections() {
    // TODO Auto-generated method stub
    return null;
}
公共类MySimpleArrayAdapter扩展ArrayAdapter实现SectionIndexer{
私人最终语境;
私有最终ArrayList值;
字母索引;
公共MySimpleArrayAdapter(上下文上下文、光标、ArrayList值){
super(context,android.R.layout.simple\u list\u item\u 1,value);
//TODO自动生成的构造函数存根
this.context=上下文;
这个值=值;
索引=新的字母索引器(游标,游标。getColumnIndex(常数。产品名称),“ABCDEFGHIJKLMNOPQRSTUVWXYZ”);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图行视图=充气机。充气(R.layout.single_项目,父项,false);
ListClass lc=values.get(位置);
TextView textView1=(TextView)rowView.findViewById(R.id.textView1);
TextView textView2=(TextView)rowView.findViewById(R.id.textView2);
TextView textView3=(TextView)rowView.findViewById(R.id.textView3);
TextView textView4=(TextView)rowView.findViewById(R.id.textView4);
TextView textView5=(TextView)rowView.findViewById(R.id.textView5);
TextView textView6=(TextView)rowView.findViewById(R.id.textView6);
textView1.setText(lc.Product);
textView2.setText(lc.MRP);
textView3.setText(信用证数量);
textView4.setText(lc.PRO);
textView5.setText(lc.SCH);
textView6.setText(lc.ACT);
返回行视图;
}
@凌驾
公共整型整型getPositionForSection(整型arg0){
//TODO自动生成的方法存根
返回0;
}
@凌驾
public int getSectionForPosition(int arg0){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共对象[]getSections(){
//TODO自动生成的方法存根
返回null;
}
有人能帮我进一步操作吗?

请尝试以下代码:

public class SideIndex extends Activity {
    private GestureDetector mGestureDetector;

    // x and y coordinates within our side index
    private static float sideIndexX;
    private static float sideIndexY;

    // height of side index
    private int sideIndexHeight;

    // number of items in the side index
    private int indexListSize;

    // list with items for side index
    private ArrayList<Object[]> indexList = null;

    // an array with countries to display in the list
    static String[] COUNTRIES = new String[] { "East Timor", "Ecuador",
            "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
            "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji",
            "Finland", "Afghanistan", "Albania", "Algeria", "American Samoa",
            "Andorra", "Angola", "Anguilla", "Antarctica",
            "Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
            "Australia", "Austria", "Azerbaijan", "Bahrain", "Bangladesh",
            "Barbados", "Belarus", "Belgium", "Monaco", "Mongolia",
            "North Korea","Northern Marianas", "Norway", "Oman", "Pakistan", "Palau","Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines","Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar","French Southern Territories", "Gabon", "Georgia", "Germany","Ghana", "Gibraltar", "Greece", "Greenland", "Grenada","Malawi", "Malaysia","Maldives", "Mali", "Malta", "Marshall Islands", "Yemen","Yugoslavia", "Zambia", "Zimbabwe" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // don't forget to sort our array (in case it's not sorted)
        Arrays.sort(COUNTRIES);

        final ListView lv1 = (ListView) findViewById(R.id.ListView01);
        lv1.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, COUNTRIES));
        mGestureDetector = new GestureDetector(this,
                new SideIndexGestureListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector.onTouchEvent(event)) {
            return true;
        } else {
            return false;
        }
    }

    private ArrayList<Object[]> createIndex(String[] strArr) {
        ArrayList<Object[]> tmpIndexList = new ArrayList<Object[]>();
        Object[] tmpIndexItem = null;

        int tmpPos = 0;
        String tmpLetter = "";
        String currentLetter = null;
        String strItem = null;

        for (int j = 0; j < strArr.length; j++) {
            strItem = strArr[j];
            currentLetter = strItem.substring(0, 1);

            // every time new letters comes
            // save it to index list
            if (!currentLetter.equals(tmpLetter)) {
                tmpIndexItem = new Object[3];
                tmpIndexItem[0] = tmpLetter;
                tmpIndexItem[1] = tmpPos - 1;
                tmpIndexItem[2] = j - 1;

                tmpLetter = currentLetter;
                tmpPos = j + 1;

                tmpIndexList.add(tmpIndexItem);
            }
        }

        // save also last letter
        tmpIndexItem = new Object[3];
        tmpIndexItem[0] = tmpLetter;
        tmpIndexItem[1] = tmpPos - 1;
        tmpIndexItem[2] = strArr.length - 1;
        tmpIndexList.add(tmpIndexItem);

        // and remove first temporary empty entry
        if (tmpIndexList != null && tmpIndexList.size() > 0) {
            tmpIndexList.remove(0);
        }

        return tmpIndexList;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        final ListView listView = (ListView) findViewById(R.id.ListView01);
        LinearLayout sideIndex = (LinearLayout) findViewById(R.id.sideIndex);
        sideIndexHeight = sideIndex.getHeight();
        sideIndex.removeAllViews();

        // TextView for every visible item
        TextView tmpTV = null;

        // we'll create the index list
        indexList = createIndex(COUNTRIES);

        // number of items in the index List
        indexListSize = indexList.size();

        // maximal number of item, which could be displayed
        int indexMaxSize = (int) Math.floor(sideIndex.getHeight() / 20);

        int tmpIndexListSize = indexListSize;

        // handling that case when indexListSize > indexMaxSize
        while (tmpIndexListSize > indexMaxSize) {
            tmpIndexListSize = tmpIndexListSize / 2;
        }

        // computing delta (only a part of items will be displayed to save a
        // place)
        double delta = indexListSize / tmpIndexListSize;

        String tmpLetter = null;
        Object[] tmpIndexItem = null;

        // show every m-th letter
        for (double i = 1; i <= indexListSize; i = i + 1) {
            tmpIndexItem = indexList.get((int) i - 1);
            tmpLetter = tmpIndexItem[0].toString();
            tmpTV = new TextView(this);
            tmpTV.setText(tmpLetter);
            tmpTV.setGravity(Gravity.CENTER);
            tmpTV.setTextSize(15);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, 1);
            tmpTV.setLayoutParams(params);
            sideIndex.addView(tmpTV);
        }

        // and set a touch listener for it
        sideIndex.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // now you know coordinates of touch
                sideIndexX = event.getX();
                sideIndexY = event.getY();

                // and can display a proper item it country list
                displayListItem();

                return false;
            }
        });
    }

    class SideIndexGestureListener extends
            GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            // we know already coordinates of first touch
            // we know as well a scroll distance
            sideIndexX = sideIndexX - distanceX;
            sideIndexY = sideIndexY - distanceY;

            // when the user scrolls within our side index
            // we can show for every position in it a proper
            // item in the country list
            if (sideIndexX >= 0 && sideIndexY >= 0) {
                displayListItem();
            }

            return super.onScroll(e1, e2, distanceX, distanceY);
        }
    }

    public void displayListItem() {
        // compute number of pixels for every side index item
        double pixelPerIndexItem = (double) sideIndexHeight / indexListSize;

        // compute the item index for given event position belongs to
        int itemPosition = (int) (sideIndexY / pixelPerIndexItem);

        // compute minimal position for the item in the list
        int minPosition = (int) (itemPosition * pixelPerIndexItem);

        // get the item (we can do it since we know item index)
        Object[] indexItem = indexList.get(itemPosition);

        // and compute the proper item in the country list
        int indexMin = Integer.parseInt(indexItem[1].toString());
        int indexMax = Integer.parseInt(indexItem[2].toString());
        int indexDelta = Math.max(1, indexMax - indexMin);

        double pixelPerSubitem = pixelPerIndexItem / indexDelta;
        int subitemPosition = (int) (indexMin + (sideIndexY - minPosition)
                / pixelPerSubitem);

        ListView listView = (ListView) findViewById(R.id.ListView01);
        listView.setSelection(subitemPosition);
    }
}
公共类侧索引扩展活动{
私人手势检测器mGestureDetector;
//我方索引内的x和y坐标
私有静态浮动sideIndex;
私有静态浮动侧索引;
//侧面索引高度
私有内部索引;
//边索引中的项目数
私有int索引大小;
//包含边索引项的列表
private ArrayList indexList=null;
//包含要在列表中显示的国家/地区的数组
静态字符串[]国家=新字符串[]{“东帝汶”、“厄瓜多尔”,
“埃及”、“萨尔瓦多”、“赤道几内亚”、“厄立特里亚”、“爱沙尼亚”,
“埃塞俄比亚”、“法罗群岛”、“福克兰群岛”、“斐济”,
“芬兰”、“阿富汗”、“阿尔巴尼亚”、“阿尔及利亚”、“美属萨摩亚”,
“安道尔”、“安哥拉”、“安圭拉”、“南极洲”,
“安提瓜和巴布达”、“阿根廷”、“亚美尼亚”、“阿鲁巴”,
“澳大利亚”、“奥地利”、“阿塞拜疆”、“巴林”、“孟加拉国”,
“巴巴多斯”、“白俄罗斯”、“比利时”、“摩纳哥”、“蒙古”,
“朝鲜”、“北马里亚纳群岛”、“挪威”、“阿曼”、“巴基斯坦”、“帕劳”、“巴拿马”、“巴布亚新几内亚”、“巴拉圭”、“秘鲁”、“菲律宾”、“皮特凯恩群岛”、“波兰”、“葡萄牙”、“波多黎各”、“卡塔尔”、“法国南部领土”、“加蓬”、“格鲁吉亚”、“德国”、“加纳”、“直布罗陀”、“希腊”、“格陵兰”、“格林纳达”、“马拉维”、“马来西亚”、“马尔代夫”“马里”、“马耳他”、“马绍尔群岛”、“也门”、“南斯拉夫”、“赞比亚”、“津巴布韦”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//不要忘记对数组进行排序(以防未排序)
数组。排序(国家);
最终ListView lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(新阵列适配器),
android.R.layout.simple_list_item_1,COUNTRIES));
mGestureDetector=新的GestureDetector(此,
新的SideIndexGestureListener());
}
@凌驾
公共布尔onTouchEvent(运动事件){
if(mGestureDetector.onTouchEvent(事件)){
返回true;
}否则{
返回false;
}
}
私有ArrayList createIndex(字符串[]strArr){
ArrayList tmpIndexList=新的ArrayList();
对象[]tmpIndexItem=null;
int-tmpPos=0;
字符串tmpLetter=“”;
字符串currentLetter=null;
字符串strItem=null;
对于(int j=0;j0){
tmpIndexList.remove(0);
}
返回tmpIndexList;
}
@凌驾
WindowFocusChanged上的公共无效(布尔hasFocus){
super.onWindowFocusChanged(hasFocus);
最终ListView ListView=(ListView)findViewById(R.id.ListView01);
LinearLayout sideIndex=(LinearLayout)findViewB