使用intent

时间:2017-01-25 11:57:46

标签: android google-maps android-intent

我在MainActivity中有一个listview,它有一个调用MapActivity的OnItemClickListener方法。 Map Activity从CustomAdapter(JSON)获取纬度和经度。目标是当单击列表中的一个项目时,它打开MapActivity并从CustomAdapter获取经度和纬度。我现在的问题是MapActivity直接启动而不点击项目。

public class MainActivity extends Activity{
private static final String TAG = MainActivity.class.getSimpleName();

// resturants json url
private static final String url = "http://w99ltestapp.w99l.net/w99ltestapp/webservice/?link=getRest";
private ProgressDialog pDialog;
private List<Resturant> resturantList = new ArrayList<Resturant>();
private ListView listView;
private CustomListAdapter adapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, resturantList);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(view.getContext(), MapsActivity.class);
            startActivity(intent);
        }});

CustomAdapter

public class CustomListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Resturant> resturantItems;


    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<Resturant> resturantItems) {
        this.activity = activity;
        this.resturantItems = resturantItems;   }

@Override
public int getCount() {
    return resturantItems.size();
}

@Override
public Object getItem(int location) {
    return resturantItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.image);
    TextView name = (TextView) convertView.findViewById(R.id.name);
    TextView lat = (TextView) convertView.findViewById(R.id.lat);
    TextView lng = (TextView) convertView.findViewById(R.id.lng);


    // getting resturant data for the row
    Resturant m = resturantItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getImage(), imageLoader);

    // name
    name.setText(m.getName());

    // SETTING UP AND SENDING DATA
    lat.setText("Lat: " + String.valueOf(m.getLat()));

    lng.setText("Lng: " + String.valueOf(m.getLng()));

    double latitude = Double.parseDouble(String.valueOf(m.getLat()));
    double longitude = Double.parseDouble(String.valueOf(m.getLng()));

    LatLng fromPosition = new LatLng(latitude, longitude );
    //LatLng toPosition = new LatLng(11.145315551, 99.333455333);


    Intent intent= new Intent(convertView.getContext(), MapsActivity.class);

    Bundle args = new Bundle();
    args.putParcelable("from_position", fromPosition);
    //args.putParcelable("to_position", toPosition);

    intent.putExtra("bundle", args);

    convertView.getContext().sendBroadcast(intent);

MapsActivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private LatLng fromPosition= new LatLng(-34,151) ;
// private  LatLng toPosition ;
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    Bundle bundle = getIntent().getParcelableExtra("bundle");
    fromPosition = bundle.getParcelable("from_position");
    toPosition = bundle.getParcelable("to_position");
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney =fromPosition;
    mMap.addMarker(new MarkerOptions().position(sydney).title("resturant"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

2 个答案:

答案 0 :(得分:2)

以这种方式从CustomListAdapter

传递数据
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
someView.setOnClikListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final int position = mListView.getPositionForView((View) v.getParent());
                Intent intent = new Intent(view.getContext(), MapsActivity.class);
                Bundle bundle = new Bundle();
                //Add your data from getFactualResults method to bundle
                bundle.putString("from_position", your start point);
                bundle.putString("to_position", your end point);
                //Add the bundle to the intent
                intent.putExtras(bundle);
                startActivity(intent);));
            }
        });
 }       

以这种方式在MapsActivity中获取数据

Bundle bundle = getIntent().getExtras();

//Extract the data…
fromPosition = bundle.getString("from_position");  
toPosition = bundle.getString("to_position"); 

答案 1 :(得分:1)

为什么要将数据传递给intent,您可以使用静态变量访问应用程序中的任何位置。您还可以根据自己的要求调整意图。

只需使用fromPosition,如下所示

public static LatLng fromPosition;