0

データベースからすべての製品をリストビューに取得しようとしています。そのために、カスタム arrayadapter を使用しています。同時に、リストビューで 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;
}

誰も私をさらに進める方法を手伝ってもらえますか?

4

1 に答える 1

0

このコードで試してください:

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);
    }
}
于 2013-10-11T07:31:56.740 に答える