java中如何清除String数组中的所有元素?

发布于 2024-12-22 13:49:58 字数 30507 浏览 2 评论 0原文

由于我尝试动态加载元素,因此我收到 java.util.ConcurrentModificationException ,因此我想清除其中的所有元素。请指导我如何清除元素或如何使用片段以任何其他方式进行操作。谢谢..

String Scroll_series[] = new String[]{"     "};      

Cursor c = db.rawQuery("SELECT DISTINCT SERIES FROM CBCTABLE where YEAR=2012 AND MAKE='Audi' AND MODEL='A6'" , null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String SERIES = c.getString(c.getColumnIndex("series"));
                    series_value.add(SERIES);
                }while (c.moveToNext());
            } 
        }

        Scroll_series = series_value.toArray(new String[series_value.size()]);

这是我的完整代码。

public class Scroller extends Activity
{
private static final String TAG = "TAG";
    // TODO: Externalize string-array
    String value_year="";
    String value_make="";
    String value_model="";
    String value_series="";
    String value_style="";
    String Scroll_year[] = new String[]{"2012", "2011", "2010", "2009", "2008", "2007","2006","2005","2004"};
    String Scroll_make[] = new String[]{"       "};

    protected SQLiteDatabase db;
    List<String> year_value = new ArrayList<String>();

    List<String> model_value = new ArrayList<String>();
    List<String> series_value = new ArrayList<String>();
    List<String> style_value = new ArrayList<String>();

    // Scroll scrolled flag
    private boolean ScrollScrolled = false;

    private TextView text;
    private EditText text1;
    private EditText text2;

    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.scroller);
             SQLiteAdapter helper = new SQLiteAdapter(this); 
             db = helper.getWritableDatabase(); 

            // drillyear();
            // drillmake();


            text1 = (EditText) this.findViewById(R.id.r1);
            text2 = (EditText) this.findViewById(R.id.r2);



            initScroll1(R.id.p1);
            initScroll2(R.id.p2);

            updateStatus();


        }

    // Scroll scrolled listener
    OnScrollScrollListener scrolledListener = new OnScrollScrollListener()
        {
            public void onScrollStarts(ScrollView Scroll)
                {
                    ScrollScrolled = true;
                }

            public void onScrollEnds(ScrollView Scroll)
                {
                    ScrollScrolled = false;

                    updateStatus();
                }
        };

    // Scroll changed listener
    public OnScrollChangedListener changedListener = new OnScrollChangedListener()
        {
            public void onChanged(ScrollView Scroll, int oldValue, int newValue)
                {
                    if (!ScrollScrolled)
                        {

                            updateStatus();
                        }
                }
        };


    private void updateStatus()
        {
        text1.setText(Scroll_year[getWheel(R.id.p1).getCurrentItem()]);
        text2.setText(Scroll_make[getWheel(R.id.p2).getCurrentItem()]);

        value_year = Scroll_year[getWheel(R.id.p1).getCurrentItem()];
        value_make = Scroll_make[getWheel(R.id.p2).getCurrentItem()];

        initScroll1(R.id.p1);
        }

    private ScrollView getWheel(int id)
    {
        return (ScrollView) findViewById(id);
    }

    private int getWheelValue(int id)
    {
        return getWheel(id).getCurrentItem();
    }

    private void initScroll1(int id)
        {
            ScrollView Scroll = (ScrollView) findViewById(id);
            Scroll.setAdapter(new ArrayScrollAdapter<String>(Scroll_year));
            Scroll.setVisibleItems(4);
            Scroll.setCurrentItem(0);
            Scroll.addChangingListener(changedListener);
            Scroll.addScrollingListener(scrolledListener);
            drillmake();
        }

    private void initScroll2(int id)
        {
            ScrollView Scroll = (ScrollView) findViewById(id);
            Scroll.setAdapter(new ArrayScrollAdapter<String>(Scroll_make));
            Scroll.setVisibleItems(4);
            Scroll.setCurrentItem(0);
            Scroll.addChangingListener(changedListener);
            Scroll.addScrollingListener(scrolledListener);
        }

        private ScrollView getScroll(int id)
        {
            return (ScrollView) findViewById(id);
        }


    private int getScrollValue(int id)
        {
            return getScroll(id).getCurrentItem();
        }

    public void drillyear()
    {
        Cursor c = db.rawQuery("SELECT DISTINCT YEAR FROM CBCTABLE " , null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String YEAR = c.getString(c.getColumnIndex("year"));
                    Log.v(TAG,YEAR);
                    //int age = c.getInt(c.getColumnIndex("Age"));
                    year_value.add(YEAR);
                }while (c.moveToNext());
            } 
        }

    Scroll_year = year_value.toArray(new String[year_value.size()]);
    }

    public void drillmake()
    {
        value_year = Scroll_year[getWheel(R.id.p1).getCurrentItem()];
        Cursor c = db.rawQuery("SELECT DISTINCT MAKE FROM CBCTABLE where YEAR="+value_year , null);
        List<String> make_value = new ArrayList<String>();
        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String MAKE = c.getString(c.getColumnIndex("make"));
                    Log.v(TAG,MAKE);
                    make_value.add(MAKE);
                }while (c.moveToNext());
            } 
        }

    Scroll_make = make_value.toArray(new String[make_value.size()]);
    }


}

我的代码的另一部分

public class ScrollView extends View
{
    /** Current value & label text color */
    private static final int VALUE_TEXT_COLOR = 0xE0000000;

    /** Items text color */
    private static final int ITEMS_TEXT_COLOR = 0xFF000000;

    /** Top and bottom shadows colors */
    private static final int[] SHADOWS_COLORS = new int[]{0xFF111111, 0x00AAAAAA, 0x00AAAAAA};

    /** Additional items height (is added to standard text item height) */
    private static final int ADDITIONAL_ITEM_HEIGHT = 20;

    /** Text size */
    private static final int TEXT_SIZE = 19;

    /** Top and bottom items offset (to hide that) */
    private static final int ITEM_OFFSET = TEXT_SIZE / 2;

    /** Additional width for items layout */
    private static final int ADDITIONAL_ITEMS_SPACE = 35;

    /** Label offset */
    private static final int LABEL_OFFSET = 13;

    /** Left and right padding value */
    private static final int PADDING = 15;

    /** Default count of visible items */
    private static final int DEF_VISIBLE_ITEMS = 15;

    // Scroll Values
    private ScrollAdapter adapter = null;
    private int currentItem = 0;

    // Widths
    private int itemsWidth = 0;
    private int labelWidth = 0;

    // Count of visible items
    private int visibleItems = DEF_VISIBLE_ITEMS;

    // Text paints
    private TextPaint itemsPaint;
    private TextPaint valuePaint;

    // Layouts
    private StaticLayout itemsLayout;
    private StaticLayout labelLayout;
    private StaticLayout valueLayout;

    // Label & background
    private String label;
    private Drawable centerDrawable;

    // Shadows drawables
    private GradientDrawable topShadow;
    private GradientDrawable bottomShadow;

    // Last touch Y position
    private float lastYTouch;

    // scrolling
    private boolean isScrollingPerformed;

    // listeners
    private final List<OnScrollChangedListener> changingListeners = new LinkedList<OnScrollChangedListener>();
    private final List<OnScrollScrollListener> scrollingListeners = new LinkedList<OnScrollScrollListener>();

    /**
     * Constructor
     */
    public ScrollView(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
        }

    /**
     * Constructor
     */
    public ScrollView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }

    /**
     * Constructor
     */
    public ScrollView(Context context)
        {
            super(context);
        }

    /**
     * Gets Scroll adapter
     * 
     * @return the adapter
     */
    public ScrollAdapter getAdapter()
        {
            return adapter;
        }

    /**
     * Sets whell adapter
     * 
     * @param adapter
     *          the new Scroll adapter
     */
    public void setAdapter(ScrollAdapter adapter)
        {
            this.adapter = adapter;
            invalidate();
        }

    /**
     * Gets count of visible items
     * 
     * @return the count of visible items
     */
    public int getVisibleItems()
        {
            return visibleItems;
        }

    /**
     * Sets count of visible items
     * 
     * @param count
     *          the new count
     */
    public void setVisibleItems(int count)
        {
            visibleItems = count;
            invalidate();
        }

    /**
     * Gets label
     * 
     * @return the label
     */
    public String getLabel()
        {
            return label;
        }

    /**
     * Sets label
     * 
     * @param newLabel
     *          the label to set
     */
    public void setLabel(String newLabel)
        {
            if (label == null || !label.equals(newLabel))
                {
                    label = newLabel;
                    labelLayout = null;
                    invalidate();
                }
        }

    /**
     * Adds Scroll changing listener
     * 
     * @param listener
     *          the listener
     */
    public void addChangingListener(OnScrollChangedListener listener)
        {
            changingListeners.add(listener);
        }

    /**
     * Removes Scroll changing listener
     * 
     * @param listener
     *          the listener
     */
    public void removeChangingListener(OnScrollChangedListener listener)
        {
            changingListeners.remove(listener);
        }

    /**
     * Notifies changing listeners
     * 
     * @param oldValue
     *          the old Scroll value
     * @param newValue
     *          the new Scroll value
     */
    protected void notifyChangingListeners(int oldValue, int newValue)
        {
            for (OnScrollChangedListener listener : changingListeners)
                {
                    listener.onChanged(this, oldValue, newValue);
                }
        }

    /**
     * Adds Scroll scrolling listener
     * 
     * @param listener
     *          the listener
     */
    public void addScrollingListener(OnScrollScrollListener listener)
        {
            scrollingListeners.add(listener);
        }

    /**
     * Removes Scroll scrolling listener
     * 
     * @param listener
     *          the listener
     */
    public void removeScrollingListener(OnScrollScrollListener listener)
        {
            scrollingListeners.remove(listener);
        }

    /**
     * Notifies listeners about starting scrolling
     */
    protected void notifyScrollingListenersAboutStart()
        {
            for (OnScrollScrollListener listener : scrollingListeners)
                {
                    listener.onScrollStarts(this);
                }
        }

    /**
     * Notifies listeners about ending scrolling
     */
    protected void notifyScrollingListenersAboutEnd()
        {
            for (OnScrollScrollListener listener : scrollingListeners)
                {
                    listener.onScrollEnds(this);
                }
        }

    /**
     * Gets current value
     * 
     * @return the current value
     */
    public int getCurrentItem()
        {
            return currentItem;
        }

    /**
     * Sets the current item
     * 
     * @param index
     *          the item index
     */
    public void setCurrentItem(int index)
        {
            if (index != currentItem)
                {
                    itemsLayout = null;
                    valueLayout = null;

                    int old = currentItem;
                    currentItem = index;

                    notifyChangingListeners(old, currentItem);

                    invalidate();
                }
        }

    /**
     * Initializes resources
     */
    private void initResourcesIfNecessary()
        {
            if (itemsPaint == null)
                {
                    itemsPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
                    // itemsPaint.density = getResources().getDisplayMetrics().density;
                    itemsPaint.setTextSize(TEXT_SIZE);
                }

            if (valuePaint == null)
                {
                    valuePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
                    // valuePaint.density = getResources().getDisplayMetrics().density;
                    valuePaint.setTextSize(TEXT_SIZE);
                    valuePaint.setShadowLayer(0.5f, 0, 0.5f, 0xFFFFFFFF);
                }

            if (centerDrawable == null)
                {
                    centerDrawable = getContext().getResources().getDrawable(R.drawable.wheel_val);
                }

            if (topShadow == null)
                {
                    topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
                }

            if (bottomShadow == null)
                {
                    bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
                }

            setBackgroundResource(R.drawable.wheel_bg);
        }

    /**
     * Calculates desired height for layout
     * 
     * @param layout
     *          the source layout
     * @return the desired layout height
     */
    private int getDesiredHeight(Layout layout)
        {
            if (layout == null) { return 0; }

            int linecount = layout.getLineCount();
            int desired = layout.getLineTop(linecount) - ITEM_OFFSET * 2 - ADDITIONAL_ITEM_HEIGHT;

            // Check against our minimum height
            desired = Math.max(desired, getSuggestedMinimumHeight());

            return desired;
        }

    /**
     * Builds text depending on current value
     * 
     * @return the text
     */
    private String buildText()
        {
            ScrollAdapter adapter = getAdapter();
            StringBuilder itemsText = new StringBuilder();
            int addItems = visibleItems / 2;
            for (int i = currentItem - addItems; i < currentItem; i++)
                {
                    if (i >= 0 && adapter != null)
                        {
                            String text = adapter.getItem(i);
                            if (text != null)
                                {
                                    itemsText.append(text);
                                }
                        }
                    itemsText.append("\n");
                }

            itemsText.append("\n"); // here will be current value

            for (int i = currentItem + 1; i <= currentItem + addItems; i++)
                {
                    if (adapter != null && i < adapter.getItemsCount())
                        {
                            String text = adapter.getItem(i);
                            if (text != null)
                                {
                                    itemsText.append(text);
                                }
                        }
                    if (i < currentItem + addItems)
                        {
                            itemsText.append("\n");
                        }
                }
            return itemsText.toString();
        }

    /**
     * Returns the max item length that can be present
     * 
     * @return the max length
     */
    private int getMaxTextLength()
        {
            ScrollAdapter adapter = getAdapter();
            if (adapter == null) { return 0; }

            int adapterLength = adapter.getMaximumLength();
            if (adapterLength > 0) { return adapterLength; }

            String maxText = null;
            int addItems = visibleItems / 2;
            for (int i = Math.max(currentItem - addItems, 0); i < Math.min(currentItem + visibleItems, adapter.getItemsCount()); i++)
                {
                    String text = adapter.getItem(i);
                    if (text != null && (maxText == null || maxText.length() < text.length()))
                        {
                            maxText = text;
                        }
                }

            return maxText != null ? maxText.length() : 0;
        }

    /**
     * Calculates control width and creates text layouts
     * 
     * @param widthSize
     *          the input layout width
     * @param mode
     *          the layout mode
     * @return the calculated control width
     */
    private int calculateLayoutWidth(int widthSize, int mode)
        {
            initResourcesIfNecessary();

            int width = widthSize;

            int maxLength = getMaxTextLength();
            if (maxLength > 0)
                {
                    float textWidth = FloatMath.ceil(Layout.getDesiredWidth("0", itemsPaint));
                    itemsWidth = (int) (maxLength * textWidth);
                }
            else
                {
                    itemsWidth = 0;
                }
            itemsWidth += ADDITIONAL_ITEMS_SPACE; // make it some more

            labelWidth = 0;
            if (label != null && label.length() > 0)
                {
                    labelWidth = (int) FloatMath.ceil(Layout.getDesiredWidth(label, valuePaint));
                }

            boolean recalculate = false;
            if (mode == MeasureSpec.EXACTLY)
                {
                    width = widthSize;
                    recalculate = true;
                }
            else
                {
                    width = itemsWidth + labelWidth + 2 * PADDING;
                    if (labelWidth > 0)
                        {
                            width += LABEL_OFFSET;
                        }

                    // Check against our minimum width
                    width = Math.max(width, getSuggestedMinimumWidth());

                    if (mode == MeasureSpec.AT_MOST && widthSize < width)
                        {
                            width = widthSize;
                            recalculate = true;
                        }
                }

            if (recalculate)
                {
                    // recalculate width
                    int pureWidth = width - LABEL_OFFSET - 2 * PADDING;
                    if (pureWidth <= 0)
                        {
                            itemsWidth = labelWidth = 0;
                        }
                    if (labelWidth > 0)
                        {
                            double newWidthItems = (double) itemsWidth * pureWidth / (itemsWidth + labelWidth);
                            itemsWidth = (int) newWidthItems;
                            labelWidth = pureWidth - itemsWidth;
                        }
                    else
                        {
                            itemsWidth = pureWidth + LABEL_OFFSET; // no label
                        }
                }

            if (itemsWidth > 0)
                {
                    createLayouts(itemsWidth, labelWidth);
                }

            return width;
        }

    /**
     * Creates layouts
     * 
     * @param widthItems
     *          width of items layout
     * @param widthLabel
     *          width of label layout
     */
    private void createLayouts(int widthItems, int widthLabel)
        {
            if (itemsLayout == null || itemsLayout.getWidth() > widthItems)
                {
                    itemsLayout = new StaticLayout(buildText(), itemsPaint, widthItems, widthLabel > 0 ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER, 1, ADDITIONAL_ITEM_HEIGHT, false);
                }
            else
                {
                    itemsLayout.increaseWidthTo(widthItems);
                }

            if (valueLayout == null || valueLayout.getWidth() > widthItems)
                {
                    String text = getAdapter() != null ? getAdapter().getItem(currentItem) : null;
                    valueLayout = new StaticLayout(text != null ? text : "", valuePaint, widthItems, widthLabel > 0 ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER, 1, ADDITIONAL_ITEM_HEIGHT, false);
                }
            else
                {
                    valueLayout.increaseWidthTo(widthItems);
                }

            if (widthLabel > 0)
                {
                    if (labelLayout == null || labelLayout.getWidth() > widthLabel)
                        {
                            labelLayout = new StaticLayout(label, valuePaint, widthLabel, Layout.Alignment.ALIGN_NORMAL, 1, ADDITIONAL_ITEM_HEIGHT, false);
                        }
                    else
                        {
                            labelLayout.increaseWidthTo(widthLabel);
                        }
                }
        }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            int width = calculateLayoutWidth(widthSize, widthMode);

            int height;
            if (heightMode == MeasureSpec.EXACTLY)
                {
                    height = heightSize;
                }
            else
                {
                    height = getDesiredHeight(itemsLayout);

                    if (heightMode == MeasureSpec.AT_MOST)
                        {
                            height = Math.min(height, heightSize);
                        }
                }

            setMeasuredDimension(width, height);
        }

    @Override
    protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);

            if (itemsLayout == null)
                {
                    if (itemsWidth == 0)
                        {
                            calculateLayoutWidth(getWidth(), MeasureSpec.EXACTLY);
                        }
                    else
                        {
                            createLayouts(itemsWidth, labelWidth);
                        }
                }

            drawCenterRect(canvas);

            if (itemsWidth > 0)
                {
                    canvas.save();
                    // Skip padding space and hide a part of top and bottom items
                    canvas.translate(PADDING, -ITEM_OFFSET);
                    drawItems(canvas);
                    drawValue(canvas);
                    canvas.restore();
                }

            drawShadows(canvas);
        }

    /**
     * Draws shadows on top and bottom of control
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawShadows(Canvas canvas)
        {
            topShadow.setBounds(0, 0, getWidth(), getHeight() / visibleItems);
            topShadow.draw(canvas);

            bottomShadow.setBounds(0, getHeight() - getHeight() / visibleItems, getWidth(), getHeight());
            bottomShadow.draw(canvas);
        }

    /**
     * Draws value and label layout
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawValue(Canvas canvas)
        {
            valuePaint.setColor(VALUE_TEXT_COLOR);
            valuePaint.drawableState = getDrawableState();

            Rect bounds = new Rect();
            itemsLayout.getLineBounds(visibleItems / 2, bounds);

            // draw label
            if (labelLayout != null)
                {
                    canvas.save();
                    canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top);
                    labelLayout.draw(canvas);
                    canvas.restore();
                }

            // draw current value
            canvas.save();
            canvas.translate(0, bounds.top);
            valueLayout.draw(canvas);
            canvas.restore();
        }

    /**
     * Draws items
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawItems(Canvas canvas)
        {
            itemsPaint.setColor(ITEMS_TEXT_COLOR);
            itemsPaint.drawableState = getDrawableState();
            itemsLayout.draw(canvas);
        }

    /**
     * Draws rect for current value
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawCenterRect(Canvas canvas)
        {
            int center = getHeight() / 2;
            int offset = getHeight() / visibleItems / 2;
            centerDrawable.setBounds(0, center - offset, getWidth(), center + offset);
            centerDrawable.draw(canvas);
        }

    @Override
    public boolean onTouchEvent(MotionEvent event)
        {
            ScrollAdapter adapter = getAdapter();
            if (adapter == null) { return true; }

            switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN :
                        lastYTouch = event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE :
                        if (!isScrollingPerformed)
                            {
                                isScrollingPerformed = true;
                                notifyScrollingListenersAboutStart();
                            }
                        float delta = event.getY() - lastYTouch;
                        int count = (int) (visibleItems * delta / getHeight());
                        int pos = currentItem - count;
                        pos = Math.max(pos, 0);
                        pos = Math.min(pos, adapter.getItemsCount() - 1);
                        if (pos != currentItem)
                            {
                                lastYTouch = event.getY();
                                setCurrentItem(pos);
                            }
                        break;

                    case MotionEvent.ACTION_UP :
                        if (isScrollingPerformed)
                            {
                                notifyScrollingListenersAboutEnd();
                                isScrollingPerformed = false;
                            }
                        break;
                }
            return true;
        }
}

这是我的登录执行

12-21 10:43:26.174: E/AndroidRuntime(591): 致命异常: main
12-21 10:43:26.174: E/AndroidRuntime(591): java.util.ConcurrentModificationException

12-21 10:43:26.174: E/AndroidRuntime(591): at java.util.LinkedList$LinkIterator.next(LinkedList.java:124)     

12-21 10:43:26.174: E/AndroidRuntime(591): at com.kk.scroll.ScrollView.notifyChangingListeners(ScrollView.java:220)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.scroll.ScrollView.setCurrentItem(ScrollView.java:296)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.SellMyCar.Scroller.initScroll1(Scroller.java:135)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.SellMyCar.Scroller.updateStatus(Scroller.java:117)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.SellMyCar.Scroller.access$1(Scroller.java:109)

12-21 10:43:26.174: E/AndroidRuntime(591): at com.kk.SellMyCar.Scroller$1.onScrollEnds(Scroller.java:91)

12-21 10:43:26.174: E/AndroidRuntime(591): at com.kk.scroll.ScrollView.notifyScrollingListenersAboutEnd(ScrollView.java:266)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.scroll.ScrollView.onTouchEvent(ScrollView.java:722)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.View.dispatchTouchEvent(View.java:3766)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.app.Activity.dispatchTouchEvent(Activity.java:2086)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.os.Handler.dispatchMessage(Handler.java:99)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.os.Looper.loop(Looper.java:123)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.app.ActivityThread.main(ActivityThread.java:4627)

12-21 10:43:26.174: E/AndroidRuntime(591):  at java.lang.reflect.Method.invokeNative(Native Method)

12-21 10:43:26.174: E/AndroidRuntime(591):  at java.lang.reflect.Method.invoke(Method.java:521)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

12-21 10:43:26.174: E/AndroidRuntime(591):  at dalvik.system.NativeStart.main(Native Method)

I am getting java.util.ConcurrentModificationException since I am trying to load the elements dynamically, so I would like to clear all the elements in it. Kindly guide me on this how to clear on the elements or how can I do in any other way with a snippet. Thanks..

String Scroll_series[] = new String[]{"     "};      

Cursor c = db.rawQuery("SELECT DISTINCT SERIES FROM CBCTABLE where YEAR=2012 AND MAKE='Audi' AND MODEL='A6'" , null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String SERIES = c.getString(c.getColumnIndex("series"));
                    series_value.add(SERIES);
                }while (c.moveToNext());
            } 
        }

        Scroll_series = series_value.toArray(new String[series_value.size()]);

This is my complete code.

public class Scroller extends Activity
{
private static final String TAG = "TAG";
    // TODO: Externalize string-array
    String value_year="";
    String value_make="";
    String value_model="";
    String value_series="";
    String value_style="";
    String Scroll_year[] = new String[]{"2012", "2011", "2010", "2009", "2008", "2007","2006","2005","2004"};
    String Scroll_make[] = new String[]{"       "};

    protected SQLiteDatabase db;
    List<String> year_value = new ArrayList<String>();

    List<String> model_value = new ArrayList<String>();
    List<String> series_value = new ArrayList<String>();
    List<String> style_value = new ArrayList<String>();

    // Scroll scrolled flag
    private boolean ScrollScrolled = false;

    private TextView text;
    private EditText text1;
    private EditText text2;

    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.scroller);
             SQLiteAdapter helper = new SQLiteAdapter(this); 
             db = helper.getWritableDatabase(); 

            // drillyear();
            // drillmake();


            text1 = (EditText) this.findViewById(R.id.r1);
            text2 = (EditText) this.findViewById(R.id.r2);



            initScroll1(R.id.p1);
            initScroll2(R.id.p2);

            updateStatus();


        }

    // Scroll scrolled listener
    OnScrollScrollListener scrolledListener = new OnScrollScrollListener()
        {
            public void onScrollStarts(ScrollView Scroll)
                {
                    ScrollScrolled = true;
                }

            public void onScrollEnds(ScrollView Scroll)
                {
                    ScrollScrolled = false;

                    updateStatus();
                }
        };

    // Scroll changed listener
    public OnScrollChangedListener changedListener = new OnScrollChangedListener()
        {
            public void onChanged(ScrollView Scroll, int oldValue, int newValue)
                {
                    if (!ScrollScrolled)
                        {

                            updateStatus();
                        }
                }
        };


    private void updateStatus()
        {
        text1.setText(Scroll_year[getWheel(R.id.p1).getCurrentItem()]);
        text2.setText(Scroll_make[getWheel(R.id.p2).getCurrentItem()]);

        value_year = Scroll_year[getWheel(R.id.p1).getCurrentItem()];
        value_make = Scroll_make[getWheel(R.id.p2).getCurrentItem()];

        initScroll1(R.id.p1);
        }

    private ScrollView getWheel(int id)
    {
        return (ScrollView) findViewById(id);
    }

    private int getWheelValue(int id)
    {
        return getWheel(id).getCurrentItem();
    }

    private void initScroll1(int id)
        {
            ScrollView Scroll = (ScrollView) findViewById(id);
            Scroll.setAdapter(new ArrayScrollAdapter<String>(Scroll_year));
            Scroll.setVisibleItems(4);
            Scroll.setCurrentItem(0);
            Scroll.addChangingListener(changedListener);
            Scroll.addScrollingListener(scrolledListener);
            drillmake();
        }

    private void initScroll2(int id)
        {
            ScrollView Scroll = (ScrollView) findViewById(id);
            Scroll.setAdapter(new ArrayScrollAdapter<String>(Scroll_make));
            Scroll.setVisibleItems(4);
            Scroll.setCurrentItem(0);
            Scroll.addChangingListener(changedListener);
            Scroll.addScrollingListener(scrolledListener);
        }

        private ScrollView getScroll(int id)
        {
            return (ScrollView) findViewById(id);
        }


    private int getScrollValue(int id)
        {
            return getScroll(id).getCurrentItem();
        }

    public void drillyear()
    {
        Cursor c = db.rawQuery("SELECT DISTINCT YEAR FROM CBCTABLE " , null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String YEAR = c.getString(c.getColumnIndex("year"));
                    Log.v(TAG,YEAR);
                    //int age = c.getInt(c.getColumnIndex("Age"));
                    year_value.add(YEAR);
                }while (c.moveToNext());
            } 
        }

    Scroll_year = year_value.toArray(new String[year_value.size()]);
    }

    public void drillmake()
    {
        value_year = Scroll_year[getWheel(R.id.p1).getCurrentItem()];
        Cursor c = db.rawQuery("SELECT DISTINCT MAKE FROM CBCTABLE where YEAR="+value_year , null);
        List<String> make_value = new ArrayList<String>();
        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String MAKE = c.getString(c.getColumnIndex("make"));
                    Log.v(TAG,MAKE);
                    make_value.add(MAKE);
                }while (c.moveToNext());
            } 
        }

    Scroll_make = make_value.toArray(new String[make_value.size()]);
    }


}

The other part of my code

public class ScrollView extends View
{
    /** Current value & label text color */
    private static final int VALUE_TEXT_COLOR = 0xE0000000;

    /** Items text color */
    private static final int ITEMS_TEXT_COLOR = 0xFF000000;

    /** Top and bottom shadows colors */
    private static final int[] SHADOWS_COLORS = new int[]{0xFF111111, 0x00AAAAAA, 0x00AAAAAA};

    /** Additional items height (is added to standard text item height) */
    private static final int ADDITIONAL_ITEM_HEIGHT = 20;

    /** Text size */
    private static final int TEXT_SIZE = 19;

    /** Top and bottom items offset (to hide that) */
    private static final int ITEM_OFFSET = TEXT_SIZE / 2;

    /** Additional width for items layout */
    private static final int ADDITIONAL_ITEMS_SPACE = 35;

    /** Label offset */
    private static final int LABEL_OFFSET = 13;

    /** Left and right padding value */
    private static final int PADDING = 15;

    /** Default count of visible items */
    private static final int DEF_VISIBLE_ITEMS = 15;

    // Scroll Values
    private ScrollAdapter adapter = null;
    private int currentItem = 0;

    // Widths
    private int itemsWidth = 0;
    private int labelWidth = 0;

    // Count of visible items
    private int visibleItems = DEF_VISIBLE_ITEMS;

    // Text paints
    private TextPaint itemsPaint;
    private TextPaint valuePaint;

    // Layouts
    private StaticLayout itemsLayout;
    private StaticLayout labelLayout;
    private StaticLayout valueLayout;

    // Label & background
    private String label;
    private Drawable centerDrawable;

    // Shadows drawables
    private GradientDrawable topShadow;
    private GradientDrawable bottomShadow;

    // Last touch Y position
    private float lastYTouch;

    // scrolling
    private boolean isScrollingPerformed;

    // listeners
    private final List<OnScrollChangedListener> changingListeners = new LinkedList<OnScrollChangedListener>();
    private final List<OnScrollScrollListener> scrollingListeners = new LinkedList<OnScrollScrollListener>();

    /**
     * Constructor
     */
    public ScrollView(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
        }

    /**
     * Constructor
     */
    public ScrollView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }

    /**
     * Constructor
     */
    public ScrollView(Context context)
        {
            super(context);
        }

    /**
     * Gets Scroll adapter
     * 
     * @return the adapter
     */
    public ScrollAdapter getAdapter()
        {
            return adapter;
        }

    /**
     * Sets whell adapter
     * 
     * @param adapter
     *          the new Scroll adapter
     */
    public void setAdapter(ScrollAdapter adapter)
        {
            this.adapter = adapter;
            invalidate();
        }

    /**
     * Gets count of visible items
     * 
     * @return the count of visible items
     */
    public int getVisibleItems()
        {
            return visibleItems;
        }

    /**
     * Sets count of visible items
     * 
     * @param count
     *          the new count
     */
    public void setVisibleItems(int count)
        {
            visibleItems = count;
            invalidate();
        }

    /**
     * Gets label
     * 
     * @return the label
     */
    public String getLabel()
        {
            return label;
        }

    /**
     * Sets label
     * 
     * @param newLabel
     *          the label to set
     */
    public void setLabel(String newLabel)
        {
            if (label == null || !label.equals(newLabel))
                {
                    label = newLabel;
                    labelLayout = null;
                    invalidate();
                }
        }

    /**
     * Adds Scroll changing listener
     * 
     * @param listener
     *          the listener
     */
    public void addChangingListener(OnScrollChangedListener listener)
        {
            changingListeners.add(listener);
        }

    /**
     * Removes Scroll changing listener
     * 
     * @param listener
     *          the listener
     */
    public void removeChangingListener(OnScrollChangedListener listener)
        {
            changingListeners.remove(listener);
        }

    /**
     * Notifies changing listeners
     * 
     * @param oldValue
     *          the old Scroll value
     * @param newValue
     *          the new Scroll value
     */
    protected void notifyChangingListeners(int oldValue, int newValue)
        {
            for (OnScrollChangedListener listener : changingListeners)
                {
                    listener.onChanged(this, oldValue, newValue);
                }
        }

    /**
     * Adds Scroll scrolling listener
     * 
     * @param listener
     *          the listener
     */
    public void addScrollingListener(OnScrollScrollListener listener)
        {
            scrollingListeners.add(listener);
        }

    /**
     * Removes Scroll scrolling listener
     * 
     * @param listener
     *          the listener
     */
    public void removeScrollingListener(OnScrollScrollListener listener)
        {
            scrollingListeners.remove(listener);
        }

    /**
     * Notifies listeners about starting scrolling
     */
    protected void notifyScrollingListenersAboutStart()
        {
            for (OnScrollScrollListener listener : scrollingListeners)
                {
                    listener.onScrollStarts(this);
                }
        }

    /**
     * Notifies listeners about ending scrolling
     */
    protected void notifyScrollingListenersAboutEnd()
        {
            for (OnScrollScrollListener listener : scrollingListeners)
                {
                    listener.onScrollEnds(this);
                }
        }

    /**
     * Gets current value
     * 
     * @return the current value
     */
    public int getCurrentItem()
        {
            return currentItem;
        }

    /**
     * Sets the current item
     * 
     * @param index
     *          the item index
     */
    public void setCurrentItem(int index)
        {
            if (index != currentItem)
                {
                    itemsLayout = null;
                    valueLayout = null;

                    int old = currentItem;
                    currentItem = index;

                    notifyChangingListeners(old, currentItem);

                    invalidate();
                }
        }

    /**
     * Initializes resources
     */
    private void initResourcesIfNecessary()
        {
            if (itemsPaint == null)
                {
                    itemsPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
                    // itemsPaint.density = getResources().getDisplayMetrics().density;
                    itemsPaint.setTextSize(TEXT_SIZE);
                }

            if (valuePaint == null)
                {
                    valuePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
                    // valuePaint.density = getResources().getDisplayMetrics().density;
                    valuePaint.setTextSize(TEXT_SIZE);
                    valuePaint.setShadowLayer(0.5f, 0, 0.5f, 0xFFFFFFFF);
                }

            if (centerDrawable == null)
                {
                    centerDrawable = getContext().getResources().getDrawable(R.drawable.wheel_val);
                }

            if (topShadow == null)
                {
                    topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
                }

            if (bottomShadow == null)
                {
                    bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
                }

            setBackgroundResource(R.drawable.wheel_bg);
        }

    /**
     * Calculates desired height for layout
     * 
     * @param layout
     *          the source layout
     * @return the desired layout height
     */
    private int getDesiredHeight(Layout layout)
        {
            if (layout == null) { return 0; }

            int linecount = layout.getLineCount();
            int desired = layout.getLineTop(linecount) - ITEM_OFFSET * 2 - ADDITIONAL_ITEM_HEIGHT;

            // Check against our minimum height
            desired = Math.max(desired, getSuggestedMinimumHeight());

            return desired;
        }

    /**
     * Builds text depending on current value
     * 
     * @return the text
     */
    private String buildText()
        {
            ScrollAdapter adapter = getAdapter();
            StringBuilder itemsText = new StringBuilder();
            int addItems = visibleItems / 2;
            for (int i = currentItem - addItems; i < currentItem; i++)
                {
                    if (i >= 0 && adapter != null)
                        {
                            String text = adapter.getItem(i);
                            if (text != null)
                                {
                                    itemsText.append(text);
                                }
                        }
                    itemsText.append("\n");
                }

            itemsText.append("\n"); // here will be current value

            for (int i = currentItem + 1; i <= currentItem + addItems; i++)
                {
                    if (adapter != null && i < adapter.getItemsCount())
                        {
                            String text = adapter.getItem(i);
                            if (text != null)
                                {
                                    itemsText.append(text);
                                }
                        }
                    if (i < currentItem + addItems)
                        {
                            itemsText.append("\n");
                        }
                }
            return itemsText.toString();
        }

    /**
     * Returns the max item length that can be present
     * 
     * @return the max length
     */
    private int getMaxTextLength()
        {
            ScrollAdapter adapter = getAdapter();
            if (adapter == null) { return 0; }

            int adapterLength = adapter.getMaximumLength();
            if (adapterLength > 0) { return adapterLength; }

            String maxText = null;
            int addItems = visibleItems / 2;
            for (int i = Math.max(currentItem - addItems, 0); i < Math.min(currentItem + visibleItems, adapter.getItemsCount()); i++)
                {
                    String text = adapter.getItem(i);
                    if (text != null && (maxText == null || maxText.length() < text.length()))
                        {
                            maxText = text;
                        }
                }

            return maxText != null ? maxText.length() : 0;
        }

    /**
     * Calculates control width and creates text layouts
     * 
     * @param widthSize
     *          the input layout width
     * @param mode
     *          the layout mode
     * @return the calculated control width
     */
    private int calculateLayoutWidth(int widthSize, int mode)
        {
            initResourcesIfNecessary();

            int width = widthSize;

            int maxLength = getMaxTextLength();
            if (maxLength > 0)
                {
                    float textWidth = FloatMath.ceil(Layout.getDesiredWidth("0", itemsPaint));
                    itemsWidth = (int) (maxLength * textWidth);
                }
            else
                {
                    itemsWidth = 0;
                }
            itemsWidth += ADDITIONAL_ITEMS_SPACE; // make it some more

            labelWidth = 0;
            if (label != null && label.length() > 0)
                {
                    labelWidth = (int) FloatMath.ceil(Layout.getDesiredWidth(label, valuePaint));
                }

            boolean recalculate = false;
            if (mode == MeasureSpec.EXACTLY)
                {
                    width = widthSize;
                    recalculate = true;
                }
            else
                {
                    width = itemsWidth + labelWidth + 2 * PADDING;
                    if (labelWidth > 0)
                        {
                            width += LABEL_OFFSET;
                        }

                    // Check against our minimum width
                    width = Math.max(width, getSuggestedMinimumWidth());

                    if (mode == MeasureSpec.AT_MOST && widthSize < width)
                        {
                            width = widthSize;
                            recalculate = true;
                        }
                }

            if (recalculate)
                {
                    // recalculate width
                    int pureWidth = width - LABEL_OFFSET - 2 * PADDING;
                    if (pureWidth <= 0)
                        {
                            itemsWidth = labelWidth = 0;
                        }
                    if (labelWidth > 0)
                        {
                            double newWidthItems = (double) itemsWidth * pureWidth / (itemsWidth + labelWidth);
                            itemsWidth = (int) newWidthItems;
                            labelWidth = pureWidth - itemsWidth;
                        }
                    else
                        {
                            itemsWidth = pureWidth + LABEL_OFFSET; // no label
                        }
                }

            if (itemsWidth > 0)
                {
                    createLayouts(itemsWidth, labelWidth);
                }

            return width;
        }

    /**
     * Creates layouts
     * 
     * @param widthItems
     *          width of items layout
     * @param widthLabel
     *          width of label layout
     */
    private void createLayouts(int widthItems, int widthLabel)
        {
            if (itemsLayout == null || itemsLayout.getWidth() > widthItems)
                {
                    itemsLayout = new StaticLayout(buildText(), itemsPaint, widthItems, widthLabel > 0 ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER, 1, ADDITIONAL_ITEM_HEIGHT, false);
                }
            else
                {
                    itemsLayout.increaseWidthTo(widthItems);
                }

            if (valueLayout == null || valueLayout.getWidth() > widthItems)
                {
                    String text = getAdapter() != null ? getAdapter().getItem(currentItem) : null;
                    valueLayout = new StaticLayout(text != null ? text : "", valuePaint, widthItems, widthLabel > 0 ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_CENTER, 1, ADDITIONAL_ITEM_HEIGHT, false);
                }
            else
                {
                    valueLayout.increaseWidthTo(widthItems);
                }

            if (widthLabel > 0)
                {
                    if (labelLayout == null || labelLayout.getWidth() > widthLabel)
                        {
                            labelLayout = new StaticLayout(label, valuePaint, widthLabel, Layout.Alignment.ALIGN_NORMAL, 1, ADDITIONAL_ITEM_HEIGHT, false);
                        }
                    else
                        {
                            labelLayout.increaseWidthTo(widthLabel);
                        }
                }
        }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            int width = calculateLayoutWidth(widthSize, widthMode);

            int height;
            if (heightMode == MeasureSpec.EXACTLY)
                {
                    height = heightSize;
                }
            else
                {
                    height = getDesiredHeight(itemsLayout);

                    if (heightMode == MeasureSpec.AT_MOST)
                        {
                            height = Math.min(height, heightSize);
                        }
                }

            setMeasuredDimension(width, height);
        }

    @Override
    protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);

            if (itemsLayout == null)
                {
                    if (itemsWidth == 0)
                        {
                            calculateLayoutWidth(getWidth(), MeasureSpec.EXACTLY);
                        }
                    else
                        {
                            createLayouts(itemsWidth, labelWidth);
                        }
                }

            drawCenterRect(canvas);

            if (itemsWidth > 0)
                {
                    canvas.save();
                    // Skip padding space and hide a part of top and bottom items
                    canvas.translate(PADDING, -ITEM_OFFSET);
                    drawItems(canvas);
                    drawValue(canvas);
                    canvas.restore();
                }

            drawShadows(canvas);
        }

    /**
     * Draws shadows on top and bottom of control
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawShadows(Canvas canvas)
        {
            topShadow.setBounds(0, 0, getWidth(), getHeight() / visibleItems);
            topShadow.draw(canvas);

            bottomShadow.setBounds(0, getHeight() - getHeight() / visibleItems, getWidth(), getHeight());
            bottomShadow.draw(canvas);
        }

    /**
     * Draws value and label layout
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawValue(Canvas canvas)
        {
            valuePaint.setColor(VALUE_TEXT_COLOR);
            valuePaint.drawableState = getDrawableState();

            Rect bounds = new Rect();
            itemsLayout.getLineBounds(visibleItems / 2, bounds);

            // draw label
            if (labelLayout != null)
                {
                    canvas.save();
                    canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top);
                    labelLayout.draw(canvas);
                    canvas.restore();
                }

            // draw current value
            canvas.save();
            canvas.translate(0, bounds.top);
            valueLayout.draw(canvas);
            canvas.restore();
        }

    /**
     * Draws items
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawItems(Canvas canvas)
        {
            itemsPaint.setColor(ITEMS_TEXT_COLOR);
            itemsPaint.drawableState = getDrawableState();
            itemsLayout.draw(canvas);
        }

    /**
     * Draws rect for current value
     * 
     * @param canvas
     *          the canvas for drawing
     */
    private void drawCenterRect(Canvas canvas)
        {
            int center = getHeight() / 2;
            int offset = getHeight() / visibleItems / 2;
            centerDrawable.setBounds(0, center - offset, getWidth(), center + offset);
            centerDrawable.draw(canvas);
        }

    @Override
    public boolean onTouchEvent(MotionEvent event)
        {
            ScrollAdapter adapter = getAdapter();
            if (adapter == null) { return true; }

            switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN :
                        lastYTouch = event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE :
                        if (!isScrollingPerformed)
                            {
                                isScrollingPerformed = true;
                                notifyScrollingListenersAboutStart();
                            }
                        float delta = event.getY() - lastYTouch;
                        int count = (int) (visibleItems * delta / getHeight());
                        int pos = currentItem - count;
                        pos = Math.max(pos, 0);
                        pos = Math.min(pos, adapter.getItemsCount() - 1);
                        if (pos != currentItem)
                            {
                                lastYTouch = event.getY();
                                setCurrentItem(pos);
                            }
                        break;

                    case MotionEvent.ACTION_UP :
                        if (isScrollingPerformed)
                            {
                                notifyScrollingListenersAboutEnd();
                                isScrollingPerformed = false;
                            }
                        break;
                }
            return true;
        }
}

This is my Log on execution

12-21 10:43:26.174: E/AndroidRuntime(591): FATAL EXCEPTION: main
12-21 10:43:26.174: E/AndroidRuntime(591): java.util.ConcurrentModificationException

12-21 10:43:26.174: E/AndroidRuntime(591): at java.util.LinkedList$LinkIterator.next(LinkedList.java:124)     

12-21 10:43:26.174: E/AndroidRuntime(591): at com.kk.scroll.ScrollView.notifyChangingListeners(ScrollView.java:220)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.scroll.ScrollView.setCurrentItem(ScrollView.java:296)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.SellMyCar.Scroller.initScroll1(Scroller.java:135)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.SellMyCar.Scroller.updateStatus(Scroller.java:117)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.SellMyCar.Scroller.access$1(Scroller.java:109)

12-21 10:43:26.174: E/AndroidRuntime(591): at com.kk.SellMyCar.Scroller$1.onScrollEnds(Scroller.java:91)

12-21 10:43:26.174: E/AndroidRuntime(591): at com.kk.scroll.ScrollView.notifyScrollingListenersAboutEnd(ScrollView.java:266)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.kk.scroll.ScrollView.onTouchEvent(ScrollView.java:722)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.View.dispatchTouchEvent(View.java:3766)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.app.Activity.dispatchTouchEvent(Activity.java:2086)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.os.Handler.dispatchMessage(Handler.java:99)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.os.Looper.loop(Looper.java:123)

12-21 10:43:26.174: E/AndroidRuntime(591):  at android.app.ActivityThread.main(ActivityThread.java:4627)

12-21 10:43:26.174: E/AndroidRuntime(591):  at java.lang.reflect.Method.invokeNative(Native Method)

12-21 10:43:26.174: E/AndroidRuntime(591):  at java.lang.reflect.Method.invoke(Method.java:521)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

12-21 10:43:26.174: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

12-21 10:43:26.174: E/AndroidRuntime(591):  at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

错爱 2024-12-29 13:49:58

严格回答您的问题:

java.lang.Arrays.fill() 

提供了几种允许您清除数组内容的方法。

这很可能不能解决您的实际问题。 ConcurrentModificationException 与“清除”数组无关。

To answer strictly your question:

java.lang.Arrays.fill() 

provides several methods which allows you to clear the contents of an array.

This will most propably not solve your real problem. ConcurrentModificationException has nothing to do with "clearing" an array.

万劫不复 2024-12-29 13:49:58

ConcurrentModificationException 最可能的原因是,当您对 series_value 调用 toArray 时,您会对其进行迭代,同时通过添加来修改它元素。

您应该在每次执行此代码段时创建一个新的 series_value 实例,并且之后不要重复使用该实例。

String Scroll_series[] = new String[]{"     "};      
List<String> series_value = new ArrayList<String>(); // fresh instance
Cursor c = db.rawQuery("SELECT DISTINCT SERIES FROM CBCTABLE where YEAR=2012 AND MAKE='Audi' AND MODEL='A6'" , null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String SERIES = c.getString(c.getColumnIndex("series"));
                        series_value.add(SERIES);
                    }while (c.moveToNext());
                } 
            }

            Scroll_series = series_value.toArray(new String[series_value.size()]);

The most likely cause of ConcurrentModificationException is that you are iterating over series_value when you call toArray on it, while modifying it at the same time by adding elements.

You should create a fresh instance of series_value every time you execute this snippet, and not reuse this instance afterwards.

String Scroll_series[] = new String[]{"     "};      
List<String> series_value = new ArrayList<String>(); // fresh instance
Cursor c = db.rawQuery("SELECT DISTINCT SERIES FROM CBCTABLE where YEAR=2012 AND MAKE='Audi' AND MODEL='A6'" , null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String SERIES = c.getString(c.getColumnIndex("series"));
                        series_value.add(SERIES);
                    }while (c.moveToNext());
                } 
            }

            Scroll_series = series_value.toArray(new String[series_value.size()]);
溺ぐ爱和你が 2024-12-29 13:49:58

堆栈跟踪的这一部分是导致 CME 的关键:

... java.util.ConcurrentModificationException 
... at java.util.LinkedList$LinkIterator.next(LinkedList.java:124)     
... at com.kk.scroll.ScrollView.notifyChangingListeners(ScrollView.java:220)
... at com.kk.scroll.ScrollView.setCurrentItem(ScrollView.java:296)
... at com.kk.SellMyCar.Scroller.initScroll1(Scroller.java:135)
... at com.kk.SellMyCar.Scroller.updateStatus(Scroller.java:117)
...
... at com.kk.scroll.ScrollView.notifyScrollingListenersAboutEnd(
       ScrollView.java:266)

如果您仔细研究ConcurrentModificationException 的 API 文档 您很可能会发现在 initScroll1/2 中添加侦听器是不安全的,因为它会干扰在其中执行的迭代notifyChangingListeners

使用您提供的代码墙而不是 SSCCE ,很难说出如何解决这个问题,但据我所知可以看到,将 initScroll1/2onCreate 移动到构造函数看起来值得尝试:

public class Scroller extends Activity {
    // ...
    // add constructor that inits scrolls
    public Scroller() {
            initScroll1(R.id.p1);
            initScroll2(R.id.p2);
        }
    // ...
    @Override
    // comment out code that inits scrolls
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.scroller);
            SQLiteAdapter helper = new SQLiteAdapter(this); 
            db = helper.getWritableDatabase(); 
            // drillyear();
            // drillmake();
            text1 = (EditText) this.findViewById(R.id.r1);
            text2 = (EditText) this.findViewById(R.id.r2);
            // causes CME: initScroll1(R.id.p1);
            // causes CME: initScroll2(R.id.p2);
            updateStatus();
        }
    // ...

This part of stack trace is the key to what is causing CME:

... java.util.ConcurrentModificationException 
... at java.util.LinkedList$LinkIterator.next(LinkedList.java:124)     
... at com.kk.scroll.ScrollView.notifyChangingListeners(ScrollView.java:220)
... at com.kk.scroll.ScrollView.setCurrentItem(ScrollView.java:296)
... at com.kk.SellMyCar.Scroller.initScroll1(Scroller.java:135)
... at com.kk.SellMyCar.Scroller.updateStatus(Scroller.java:117)
...
... at com.kk.scroll.ScrollView.notifyScrollingListenersAboutEnd(
       ScrollView.java:266)

If you carefully study API documentation for ConcurrentModificationException you'll most likely discover that adding listeners in initScroll1/2 is unsafe since it interferes with iteration performed within notifyChangingListeners.

With the wall of code you provided instead of SSCCE it's rather hard to tell how to fix that but as far as I can see, moving initScroll1/2 from onCreate to constructor looks worth trying:

public class Scroller extends Activity {
    // ...
    // add constructor that inits scrolls
    public Scroller() {
            initScroll1(R.id.p1);
            initScroll2(R.id.p2);
        }
    // ...
    @Override
    // comment out code that inits scrolls
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.scroller);
            SQLiteAdapter helper = new SQLiteAdapter(this); 
            db = helper.getWritableDatabase(); 
            // drillyear();
            // drillmake();
            text1 = (EditText) this.findViewById(R.id.r1);
            text2 = (EditText) this.findViewById(R.id.r2);
            // causes CME: initScroll1(R.id.p1);
            // causes CME: initScroll2(R.id.p2);
            updateStatus();
        }
    // ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文