三个活动之间的意图错误

时间:2016-08-29 16:00:16

标签: android android-intent

我在堆栈溢出的三个活动中搜索了关于android意图的所有问题,但不幸的是没有我所面临的预期场合,第一个活动包括一个简单的按钮,但它没关系,通过该活动我通过了数据到第二个活动,我想点击textView(一个按钮)并将一些数据传递给第三个活动,总计划太长,这里是片段:

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.btn_search:
            Intent third = new Intent(this, ThirdActivity.class);
            startActivity(third);
            break;
        default:
            break;
    }
}

我期望的是单击按钮并跳转到第三个活动,但实际上每次点击它回到第一个活动,我很好奇,如果我不能在第二个活动中使用意图?但我认为这是荒谬的。有没有关于这个问题的建议。 更新:这里是全班但有很多空实现,而SDK是高德地图,所以不关心总逻辑,只关注onclick事件:

public class AroundSearchActivity extends Activity implements AMap.OnMapClickListener,
            AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter,
            View.OnClickListener, PoiSearch.OnPoiSearchListener, LocationSource, AMapLocationListener
    {
        private AMap map;
        private MapView mapView;
        private AMapLocation mapLocation;
        private AMapLocationClient mapLocationClient;
        private AMapLocationClientOption mapLocationClientOption;
        private OnLocationChangedListener locationChangedListener;
        private double latitude;
        private double longitude;
        private double previousLatitude = 0;
        private Marker currentMarker;
        private RelativeLayout detail;
        private TextView name;
        private TextView address;
        private EditText input_text;
        private PoiSearch poiSearch;
        private PoiSearch.Query query;
        private PoiResult result;
        private ArrayList<PoiItem> poiItems;
        private Marker mLastMarker;
        private Marker detailMarker;
        private MyPoiOverlay poiOverlay;
        private String searchStr;
        private int[] showMarkers = {
                R.drawable.poi_marker_1,R.drawable.poi_marker_2,R.drawable.poi_marker_3,R.drawable.poi_marker_4,R.drawable.poi_marker_5,
                R.drawable.poi_marker_6,R.drawable.poi_marker_7,R.drawable.poi_marker_8,R.drawable.poi_marker_9,R.drawable.poi_marker_10,
        };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.poiaroundsearch_activity);
            mapView = (MapView) findViewById(R.id.mapView);
            mapView.onCreate(savedInstanceState);
            init();
        }
        public void init(){
            if (map == null){
                map = mapView.getMap();
            }
            setUpMap();
            map.setOnMapClickListener(this);
            map.setOnMarkerClickListener(this);
            map.setOnInfoWindowClickListener(this);
            map.setInfoWindowAdapter(this);
            TextView search = (TextView) findViewById(R.id.btn_search);
            search.setOnClickListener(this);
            setup();
        }

        public void setup(){
            detail = (RelativeLayout) findViewById(R.id.poi_detail);
            detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                //toggle detail click event
                Toast.makeText(AroundSearchActivity.this, "hello", Toast.LENGTH_SHORT).show();
                }
            });
            name = (TextView) findViewById(R.id.poi_name);
            address = (TextView) findViewById(R.id.poi_address);
            input_text = (EditText) findViewById(R.id.input_edittext);
        }

        @Override
        protected void onResume() {
            super.onResume();
            mapView.onResume();
            searchMap();
        }

        @Override
        protected void onPause() {
            super.onPause();
            mapView.onDestroy();
            deactivate();
        }
        public void setUpMap(){
            map.setLocationSource(this);
            map.getUiSettings().setMyLocationButtonEnabled(true);
            map.setMyLocationEnabled(true);
            map.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);

        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            locationChangedListener = null;
            if (mapLocationClient != null){
                mapLocationClient.stopLocation();
                mapLocationClient.onDestroy();
            }
            mapLocationClient = null;
        }

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (locationChangedListener != null && aMapLocation != null){
                if(aMapLocation.getErrorCode() == 0){
                    latitude = aMapLocation.getLatitude();

                    longitude = aMapLocation.getLongitude();
                    locationChangedListener.onLocationChanged(aMapLocation);
                    Log.i("CurrentLocation", "latitude is " + latitude + " ,longitude is:" + longitude);//get current latitude and longitude
                    if (currentMarker != null) currentMarker = null;
                    currentMarker = map.addMarker(new MarkerOptions().
                            position(new LatLng(latitude, longitude))
                            .icon(null)
                            .draggable(true));
                    currentMarker.showInfoWindow();
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
                   if (previousLatitude == 0){
                        searchMap();
                    }else{
                        String pre = String.valueOf(previousLatitude);
                        String a = pre.substring(pre.indexOf(".") + 1, pre.indexOf(".") + 4);
                        String cur = String.valueOf(latitude);
                        String b = cur.substring(cur.indexOf(".") + 1, cur.indexOf(".") + 4);
                        if (!a.equals(b)){
                            searchMap();
                        }
                    }
                    previousLatitude = latitude;
                }else{
                    int errorCode = aMapLocation.getErrorCode();
                    Toast.makeText(this, "定位失败:"+errorCode, Toast.LENGTH_SHORT);
                }
            }
        }

        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }

        @Override
        public void activate(OnLocationChangedListener onLocationChangedListener) {
            locationChangedListener = onLocationChangedListener;
            if (mapLocationClient == null){
                mapLocationClient = new AMapLocationClient(this);
                mapLocationClientOption = new AMapLocationClientOption();
                mapLocationClient.setLocationListener(this);mapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
                mapLocationClient.setLocationOption(mapLocationClientOption);
                mapLocationClient.startLocation();
            }
        }

        @Override
        public void deactivate() {
            locationChangedListener = null;
            if (mapLocationClient != null){
                mapLocationClient.stopLocation();
                mapLocationClient.onDestroy();
            }
            mapLocationClient = null;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_search:
                    Intent third = new Intent(this, ThirdActivity.class);
                    startActivity(third);
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onInfoWindowClick(Marker marker) {

        }

        @Override
        public void onMapClick(LatLng latLng) {
            whetherToShowInfo(false);
            if (mLastMarker != null){
                resetLastMarker();
            }
        }

        @Override
        public boolean onMarkerClick(Marker marker) {
            if (marker.getObject() != null){
                //whether to show the marker detail information
                whetherToShowInfo(true);
                //get the current location
                PoiItem currentItem = (PoiItem) marker.getObject();
                //aim at the next marker and preserve the current marker
                if (mLastMarker == null){
                    mLastMarker = marker;
                }else {
                    resetLastMarker();
                    mLastMarker = marker;
                }
                detailMarker = marker;
                detailMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.poi_marker_pressed));
                setPoiMarkerContent(currentItem);
            }else {
                whetherToShowInfo(false);
                resetLastMarker();
            }
            return true;
        }

        @Override
        public void onPoiSearched(PoiResult poiResult, int errorCode) {
            if (errorCode == 1000){
                if (poiResult != null && poiResult.getQuery() != null){
                   if (poiResult.getQuery().equals(query)){//判断是不是同一组查询
                       result = poiResult;//将搜索结果赋值给result
                       poiItems = result.getPois();
                       //列出所有poi搜索结果
                       for (int i = 0; i < poiItems.size(); i++){
                           Log.i("result"+i, poiItems.get(i).getSnippet());
                       }
                       List<SuggestionCity> suggestionCities = result.getSearchSuggestionCitys();
                       if (poiItems != null && poiItems.size() > 0){
                           whetherToShowInfo(false);
                           if (mLastMarker != null) {
                               resetLastMarker();
                           }
                           if (poiOverlay != null){
                               poiOverlay.removeFromMap();
                           }
                           map.clear();
                           poiOverlay = new MyPoiOverlay(this, map, poiItems);
                           poiOverlay.addToMap();
                           poiOverlay.zoomToSpan();//move to the current scene
                                                          map.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                           .icon(BitmapDescriptorFactory.fromResource(R.drawable.point4))
                           .draggable(false)
                           .position(new LatLng(latitude, longitude)));
                       }else{
                           if (suggestionCities != null && suggestionCities.size() > 0){
                               showSuggestCity(suggestionCities);
                           }
                       }
                   }
                }else {
                    Toast.makeText(this, "Sorry! No result match your request", Toast.LENGTH_SHORT).show();
                }
            }else {
               Log.e("errorInfo", "Search failed:" + errorCode);
            }
        }

        @Override
        public void onPoiItemSearched(PoiItem poiItem, int i) {

        }

        public void whetherToShowInfo(boolean w){
            if (w){
                detail.setVisibility(View.VISIBLE);
            }else{
                detail.setVisibility(View.GONE);
            }
        }

        public void resetLastMarker(){
            int index = poiOverlay.getPoiIndex(mLastMarker);
            if (index < 10){
                mLastMarker.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), showMarkers[index])));
            }else{
                mLastMarker.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_other_highlight)));
            }
            mLastMarker = null;
        }

        public void showSuggestCity(List<SuggestionCity> suggestionCities){
            String information = "City information/n";
            for (int i = 0; i < suggestionCities.size(); i++){
                information += "City name" + suggestionCities.get(i).getCityName() +
                        ", City administrative code::" + suggestionCities.get(i).getAdCode();
            }
            Toast.makeText(this, information, Toast.LENGTH_SHORT);
        }

        public void searchMap(){
            Toast.makeText(this, searchStr, Toast.LENGTH_SHORT).show();
            Intent intent = getIntent();
            searchStr = intent.getStringExtra("keyword");
            int currentPage = 0;
            query = new PoiSearch.Query(searchStr.trim(), "", "Beijing");
            query.setPageSize(20);
            query.setPageNum(currentPage);
            LatLonPoint lp = new LatLonPoint(latitude, longitude);
            if (lp != null){
                poiSearch = new PoiSearch(this, query);
                poiSearch.setOnPoiSearchListener(this);
                poiSearch.setBound(new PoiSearch.SearchBound(lp, 2000, true));
                poiSearch.searchPOIAsyn();
            }
        }

        public void setPoiMarkerContent(PoiItem item){
            name.setText(item.getTitle());
            address.setText(item.getSnippet());
        }
    }

0 个答案:

没有答案