Android - Alert Dialog始终返回相同的值

时间:2014-02-04 07:33:29

标签: android alertdialog

我要创建的功能是应用程序将在Google地图上显示总线顶部图标,如果用户单击该图标,它将显示一个信息窗口,然后在用户单击信息窗口后,它将显示警告框以询问用户将总线顶部设置为原点停止或目的地停止,然后将数据传递到顶部文本框,但现在问题是无论我按哪个公交站点都会给出相同的公交车站号码,这是最后一个在arraylist中,我意识到原因是因为我在这里使用循环,但我不知道如何修改,请帮助我,谢谢!

public class Passing extends FragmentActivity {

private EditText Name;
private EditText Name2;
Button button1;
private GoogleMap googleMap;

public String stopLon;
public String stopLat;
public String stopName;
public String stopRoad;
public String stopCode;
public String OriginLoc;
public String DestinationLoc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_passing);
    Intent intent2 = getIntent();
    String Bus = intent2.getExtras().getString("Bus");

    DBAdapter db = new DBAdapter(this);

    setUpMapIfNeeded();

    db.open();
    String oriRoute = db.getRoute(Bus).toString();
    String route = oriRoute.substring(1, oriRoute.length() - 1);
    String[] bustop = route.split(" ");
    ArrayList<String> stopList = new ArrayList<String>(
            Arrays.asList(bustop));

    Cursor stopLoc;
    for (int i = 0; i < stopList.size(); i++) {
        stopLoc = db.getStop(stopList.get(i));
        stopLon = stopLoc.getString(1);
        stopLat = stopLoc.getString(2);
        stopName = stopLoc.getString(3);
        stopRoad = stopLoc.getString(4);
        stopCode = stopLoc.getString(5);
        double lon = Double.parseDouble(stopLon);
        double lat = Double.parseDouble(stopLat);
        MarkerOptions marker = new MarkerOptions();
        marker.position(new LatLng(lat, lon));
        marker.title(stopName);
        googleMap.addMarker(marker);
        googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            public void onInfoWindowClick(Marker marker) {
                AlertDialog.Builder alert = new AlertDialog.Builder(Passing.this);
                alert.setTitle("Trip Setting");
                alert.setMessage("Pls choose Start Location and Destination");
                alert.setPositiveButton("Origin Stop", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int which) {
                        String originStopCode = stopCode;
                        Name.setText(originStopCode);
                        OriginLoc = stopLat + "," + stopLon;
                    }
                });

                alert.setNegativeButton("Destination Stop",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int which) {
                        String destinationStopCode = stopCode;
                        Name2.setText(destinationStopCode);
                        DestinationLoc = stopLat + "," + stopLon;
                    }
                });

                // Setting Netural "Cancel" Button
                alert.setNeutralButton("Cancel",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int which) {
                        Toast.makeText(getApplicationContext(),"Please choose the Origin & Destination Stops",Toast.LENGTH_SHORT).show();
                    }
                });

                // Showing Alert Message
                alert.show();
            }
        });
    }
    db.close();

    button1 = (Button) findViewById(R.id.button1);
    button1.setEnabled(false);

    Name = (EditText) findViewById(R.id.Name);
    Name2 = (EditText) findViewById(R.id.Name2);

    Name.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            String Name1 = Name.getText().toString();
            String Name11 = Name2.getText().toString();
            if (Name1.equals("") || Name11.equals("")) {
                button1.setEnabled(false);
            } else {
                button1.setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });

    Name2.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            String Name1 = Name.getText().toString();
            String Name11 = Name2.getText().toString();

            if (Name1.equals("") || Name11.equals("")) {
                button1.setEnabled(false);
            } else {
                button1.setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.passing, menu);
    return true;
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {

    Intent intent2 = getIntent();
    String Bus = intent2.getExtras().getString("Bus");

    Intent intent3 = getIntent();
    String TripName = intent3.getExtras().getString("TripName");

    Intent intent = new Intent(this, MainPage.class);
    EditText editText = (EditText) findViewById(R.id.Name);
    EditText editText2 = (EditText) findViewById(R.id.Name2);
    String message = editText.getText().toString();
    String message2 = editText2.getText().toString();
    intent.putExtra("StartLo", OriginLoc);
    intent.putExtra("EndLo", DestinationLoc );
    intent.putExtra("Name", message);
    intent.putExtra("Name2", message2);
    intent.putExtra("BusNo", Bus);
    intent.putExtra("Trips", TripName);
    startActivity(intent);

}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiate the
    // map
    if (googleMap == null) {
        // Try to obtain the map from the SupportMapFragment
        googleMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.mapView)).getMap();
        // googleMap = getSupportFragmentManager().
        // Check if we were successful in obtaining the map
        if (googleMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    // Enable MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);
    // Get LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    // Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();
    // Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);
    // Get Current Location
    Location myLocation = locationManager.getLastKnownLocation(provider);
    // Set Map type
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    // Get latitude and longitude of current location
    double latitude = myLocation.getLatitude();
    double longtude = myLocation.getLongitude();
    // Create a LatLng object for the current location
    LatLng latlng = new LatLng(latitude, longtude);
    // Show the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
 }

 }

1 个答案:

答案 0 :(得分:0)

确定将GoogleMap googletest留在开头,然后在for循环之前添加此行:

GoogleMap googleTest = googleMap; 

然后在for循环中使用googleTest而不是googleMap

希望它有助于交配

相关问题