views:

274

answers:

1

I have an OnTouch and a setOnTouchListener that updates varibles which contain screen coord info. The problem is it doesnt seem to ever update them. On line 78, RGB.setText(test); it never changes from 0.0. If i were to move that line and the line above it into the onTouch it updates. any idea what is wrong? Thank you.

package com.evankimia.huskybus;

import com.test.huskybus.R;

import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView;

public class HuskyBus extends Activity {

TextView RGB;
private CampusMap mCampusMap;

private float startX = 0; //track x from one ACTION_MOVE to the next
private float startY = 0; //track y from one ACTION_MOVE to the next
float scrollByX = 0; //x amount to scroll by
float scrollByY = 0; //y amount to scroll by

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);



            setContentView(R.layout.main);

            RGB = (TextView) findViewById(R.id.coordBox);

            mCampusMap = (CampusMap) findViewById(R.id.map);
            mCampusMap.setOnTouchListener(new OnTouchListener() {

                                    @Override
                                    public boolean onTouch(View v, MotionEvent event) {
                                            // TODO Auto-generated method stub



                                            switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                            // Remember our initial down event location.

                            startX = event.getRawX();
                            startY = event.getRawY();
                            break;

                    case MotionEvent.ACTION_MOVE:
                            float x = event.getRawX();

                            float y = event.getRawY();
                            // Calculate move update. This will happen many times
                            // during the course of a single movement gesture.

                            scrollByX = x - startX; //move update x increment
                            scrollByY = y - startY; //move update y increment

                            startX = x; //reset initial values to latest
                            startY = y;



                            mCampusMap.invalidate();

                            break;
            }//end switch
                                            return false;
                                    }
                                    ;

            }); //end onDraw?

String test = "" + scrollByX; RGB.setText(test);

    }

 }
A: 

It should work if you do it this way:

package com.evankimia.huskybus;

import com.test.huskybus.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class HuskyBus extends Activity {
    TextView RGB;
    private CampusMap mCampusMap;

    private float startX = 0; // track x from one ACTION_MOVE to the next
    private float startY = 0; // track y from one ACTION_MOVE to the next
    float scrollByX = 0; // x amount to scroll by
    float scrollByY = 0; // y amount to scroll by

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        RGB = (TextView) findViewById(R.id.coordBox);

        mCampusMap = (CampusMap) findViewById(R.id.map);
        mCampusMap.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    // Remember our initial down event location.

                    startX = event.getRawX();
                    startY = event.getRawY();
                    break;

                case MotionEvent.ACTION_MOVE:
                    float x = event.getRawX();

                    float y = event.getRawY();
                    // Calculate move update. This will happen many times
                    // during the course of a single movement gesture.

                    scrollByX = x - startX; // move update x increment
                    scrollByY = y - startY; // move update y increment

                    startX = x; // reset initial values to latest
                    startY = y;

                    mCampusMap.invalidate();

                    break;
                }// end switch
                String test = "" + scrollByX;
                RGB.setText(test);
                return false;
            };

        }); // end onDraw?


    }
}

The problem is that you have the RGB.setText(test); outside the execution of the TouchListener.

BTW, try to organize your code before posting here on StackOverflow.

Cristian
Thanks, but what I am really trying to do is get the variables scrollByX and scrollByY accessible to the main class. I have a custom view that refers to this class by thisClass.scrollByX but because for some reason the variables dont update within the onTouchListener it never changes from 0.
Jay Smith