ギャラリーの場合、OnItemSelected Listenerのマーカーを変更しようとしています。実際には、アイテムのリストを表示するギャラリーと、ギャラリー内の尊敬されるアイテムのマーカーを表示するマップがあります。選択したときにマーカーを(青からオレンジに)変更したいギャラリーのアイテム。どうすればよいですか。このリンクのコードでタップするとマーカーが変更されます。
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ですが、onItemSelectedListenerで同じことを行う方法がわかりません。いくつかの方法を提案してください。
コード全体を提供することはできませんが、これまでに行ったことを理解できるものがいくつかあります。
オーバーレイクラスは次のとおりです。
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 ");
}
});
}