androniennn
androniennn

Reputation: 3137

Problems with return value of boolean methods

I have this function to show an AlertDialog when taping on marker:

protected boolean onTap(int index) {
        db = openHelper.getWritableDatabase();

    AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
    String[] columns_descri = new String[] {COL_DESCRI};
    Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
    if (cur.moveToPosition(index-1)) {
    String description = cur.getString(cur.getColumnIndexOrThrow(COL_DESCRI));
    dialog.setTitle("Info.");
    dialog.setMessage(description); 
    dialog.show();
    }
    db.close();
    return true;     } 

And this method to retrieve any tapped touch on map:

@Override
    public boolean onTouchEvent(MotionEvent event, MapView maMap) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == 1) {                
            GeoPoint p = maMap.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());
                Toast.makeText(getBaseContext(), 
                    p.getLatitudeE6() / 1E6 + "," + 
                    p.getLongitudeE6() /1E6 , 
                    Toast.LENGTH_SHORT).show();
        }                            
        return true;
    }       

The problem is when onTouchEvent returns TRUE i can't control the map, move it, or show the AlertDialog when taping on a marker. When it's false, i re-take control of the map, but the toast displays many time(5 or 6 times). What could be the source of this weird problem?

Upvotes: 0

Views: 1111

Answers (3)

Pratik
Pratik

Reputation: 30855

try this

@Override
    public boolean onTouchEvent(MotionEvent event, MapView maMap) 
    {   
        //---when user lifts his finger---
        boolean result = false;
        if (event.getAction() == 1) {                
            GeoPoint p = maMap.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());
                Toast.makeText(getBaseContext(), 
                    p.getLatitudeE6() / 1E6 + "," + 
                    p.getLongitudeE6() /1E6 , 
                    Toast.LENGTH_SHORT).show();
            result true;
        }                            
        //return (result | super.onTouchEvent(event, mapView));
        return result;
    }    

Upvotes: 1

IncrediApp
IncrediApp

Reputation: 10353

OnTouchEvent is called on EACH touch event. Press, drag (many events), release. Therefore your toast is showing a lot. return true only inside your if clause and return false otherwise. That should solve it.

Upvotes: 0

DeeV
DeeV

Reputation: 36035

The boolean value of the onTap and onTouch events represent whether the action was fully handled or not. If you return true, the OS will stop cascading to other views (hence why you lose control of the map). When you return false, the OS will look for other methods that can handle the touch event.

Upvotes: 2

Related Questions