lostdev
lostdev

Reputation: 736

Android and ArrayLIst Size returns 0

Strange problem I'm having here. I add to an arraylist when the user clicks on the view. I get the position and add it to an ArrayList of coordinates. I then draw a circle on the canvas where the coordinates says to do so. The Size check in onDraw always returns 0.

private static ArrayList<Coordinate> coords = new ArrayList<Coordinate>();

OnTouchEvent

...
case MotionEvent.ACTION_POINTER_UP: {
    mLastTouchX = event.getX();
    mLastTouchY = event.getY();
    this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
    break;
...

OnDraw

protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    for(int i = 0; i < this.coords.size(); i++) {
        canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
    }
}

Upvotes: 0

Views: 1496

Answers (1)

Alex Coleman
Alex Coleman

Reputation: 7326

How are you expecting to get a static field with this ? But assuming that's just some typo, try adding some logging:

case MotionEvent.ACTION_POINTER_UP: {
    mLastTouchX = event.getX();
    mLastTouchY = event.getY();
    this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
    System.out.println("Added a coordinate; new size: " + coords.size());//to see if we are adding it
    break;

And in your onDraw:

System.out.println(coords);//Just to see what all is in it
for(int i = 0; i < this.coords.size(); i++) {
    canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
}

Upvotes: 1

Related Questions