1

如果画廊,我正在尝试更改 OnItemSelected 侦听器上的标记。实际上,我有一个显示项目列表的画廊和一个显示画廊中受尊重项目的标记的地图。当我选择一个时,我想更改标记(从蓝色到橙色)画廊中的项目。我该怎么做。我已经通过此链接中的代码在点击它时更改了标记

http://www.google.com/url?q=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F7038636%2Fhow-to-change-the-marker-for-the-overlay-on-tap-for- android&sa=D&sntz=1&usg=AFQjCNFOnFJHf-f0nTea-a3Rs5hpx4U1IQ,但不知道如何使用 onItemSelected Listener 做同样的事情。请提出一些方法。

我无法提供整个代码,但这里有一些你可以理解我到目前为止所做的事情。

这是覆盖类:

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.widget.Gallery;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


/**
 * @author Anshul
 * class is responsible for creating overlays on the map
 *
 */
public class SearchCarOverlay extends ItemizedOverlay<OverlayItem> {
    Context context;
    MapView map;
    Gallery mapGallery; 
    private Drawable heart = null;
    private ArrayList<CustomItem> aList  = null;

    public SearchCarOverlay(Cursor cursor, Context m, MapView mView,
            Gallery carGallery) {
        // super(boundCenterBottom(cursor));
        super(null);
        context             = m;
        this.map            = mView;
        this.mapGallery     = carGallery;
        aList               = new ArrayList<CustomItem>(); 
        addItemsToList(cursor);

        populate();

    }

    /**
     * @param cursor
     * This method will add all the point to the list.
     */
    private void addItemsToList(Cursor cursor) {
        GeoPoint point = null;
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {

                int lat = (int) (cursor.getDouble(cursor
                        .getColumnIndex("Latitude")) * 1000000);
                System.out.println("checking for latitude and longitude*************"
                                + lat);
                int lng = (int) (cursor.getDouble(cursor
                        .getColumnIndex("Longitude")) * 1000000);
                point = new GeoPoint(lat, lng);
                aList.add(new CustomItem(point, "", "",
                        getMarker(R.drawable.heart_full), heart));

            } while (cursor.moveToNext());

        //  map.getController().animateTo(point);

        }

    }

    /**
     * @param resource
     * @return drawable
     * for getting the map markers
     */
    private Drawable getMarker(int resource) {
        Drawable marker = context.getResources().getDrawable(resource);

        marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                marker.getIntrinsicHeight());
        boundCenter(marker);
        return (marker);
    }

    @Override
    protected CustomItem createItem(int i) {

        return aList.get(i);
    }

    @Override
    public int size() {

        return aList.size();

    }

    @Override
    protected boolean onTap(int index) {
        mapGallery.setSelection( index );
        return true;
    }

    void toggleHeart() {
        CustomItem focus = (CustomItem) getFocus();

        if (focus != null) {
            focus.toggleHeart();
        }

        map.invalidate();
    }
/*
    public ArrayList< CustomItem > getListOfItems(){
        return aList;

    }*/
    /**
     * releases all the objects memory 
     */
    public void cleanUp(){
        context     =   null;
         map        =   null;
         mapGallery =   null;   
         heart      =   null;
    }

}

class CustomItem extends OverlayItem {
    Drawable marker = null;
    boolean isHeart = false;
    Drawable heart = null;

    CustomItem(GeoPoint pt, String name, String snippet, Drawable marker,
            Drawable heart) {
        super(pt, name, snippet);
System.out.println(" CustomItem  CONSTRUCTOR ");
        this.marker = marker;
        this.heart = heart;
    }

    @Override
    public void setMarker(Drawable marker) {
        System.out.println(" CustomItem  setMarker ");      
    //  setState(marker, android.R.attr.state_pressed);

    }


    @Override
    public Drawable getMarker(int stateBitset) {
        Drawable result = (isHeart ? heart : marker);
        System.out.println(" CustomItem  getMarker ");
        setState(result, stateBitset);

        return (result);
    }

    void toggleHeart() {
        System.out.println(" CustomItem  toggleHeart ");
        isHeart = !isHeart;
    }


}
`
and my fragment file which contains gallery and map

    public class SearchCarsFragment extends Fragmnent implements OnItemClickListener, OnClickListener{

MapView mapView;
private Gallery carGallery;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup v, Bundle bundle) {

        View   view    = inflater.inflate( R.layout.search_car_result, null );
        headerText     = ( TextView )view.findViewById( R.id.frag_search_result );
         carGallery    = ( Gallery )view.findViewById( R.id.car_gallery );
         mapContainer  = ( LinearLayout)view.findViewById(R.id.search_map_container );
         listOrMap     = ( Button )view.findViewById( R.id.frag_search_option );
         helpButton    = ( Button )view.findViewById( R.id.frag_search_help );

         inflaterForMap = inflater;

         InitialiseMapView();
            initialiseMapAndGallery();


        listOrMap.setOnClickListener( this );
        helpButton.setOnClickListener( this );
        return view;
    }
/**
     * used to create map view everytime we come to Searchcars fragment
     */
    private void InitialiseMapView() {

        if( mapViewFrameLayout == null ){
            mapViewFrameLayout       = ( FrameLayout )inflaterForMap.inflate( R.layout.maps, null );
            mapView =   (MapView) mapViewFrameLayout.findViewById(R.id.carhomezone);
        }else{
            mapView =   (MapView) mapViewFrameLayout.findViewById(R.id.carhomezone);
        }


    }



    /**
     * initialises the map and gallery in the screen
     */
    private void initialiseMapAndGallery() {

         dismissDialog();

         cursor = DatabaseUtil.getInstance().getSortedCarResults( Constants.SORT_BY_DEFAULT );
        if( cursor != null && cursor.getCount() > 0 ){
            headerText.setText("Search Results ("+cursor.getCount()+")");
        initialiseMap( cursor );
        initialiseGallery( cursor );
        }else{
            System.out.println("problem in getting the data from the database");
        }   

    }

/**
     * @param cursor
     * initialises the map for search results
     */
    private void initialiseMap(Cursor cursor) { 

        InitialiseMapView();
        mapContainer.addView( mapViewFrameLayout );
        //GeoPoint point = null ;


        searchCarOverlay = new SearchCarOverlay( cursor,SearchFragmentActivity.mContext,mapView,carGallery);
        mapView.getOverlays().add( searchCarOverlay );
        mapView.setSatellite( false );
        //mapView.setStreetView( true ); 
        MapController mc = mapView.getController();
        mapView.setBuiltInZoomControls( true );

        mc.setZoom( 16 );


        //mapContainer.addView(mapView);

        //mc.animateTo( point );





        mapView.invalidate();
    }


/**
     * @param cursor
     * initialises the gallery for search results
     */
    private void initialiseGallery(Cursor cursor) {
        carGallery.setAdapter( new CarGalleyAdapter( SearchFragmentActivity.mContext ,cursor ));
        carGallery.setOnItemClickListener( this );
        carGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                System.out.println(" onItem selected listenert is called"+position);            

// here i need to right the code for changinh marker onItmeselected 


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                System.out.println(" onNothingSelected selected listenert is called  ");

            }
        });


    }
4

1 回答 1

1

如果您已经实现了在单击标记时更改颜色的标记。然后把你所做的放在你的画廊的 OnclickListener 中。

于 2012-04-17T14:07:59.890 回答