【问题标题】:Sending Latlng from CustomAdapter to MapActivity using intent使用意图将 Latlng 从 CustomAdapter 发送到 MapActivity
【发布时间】:2017-01-25 11:57:46
【问题描述】:

我在 MainActivity 中有一个列表视图,它具有调用 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);
        }});

自定义适配器

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);

地图活动

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));
}

}

【问题讨论】:

    标签: android google-maps android-intent


    【解决方案1】:

    以这种方式从 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"); 
    

    【讨论】:

    • 我想先把数据从CustomAdapter传给MainActivity。然后将它们从 MainActivity 传递给 MapActivity
    • 在任何视图的 getView() 方法中写入 setOnClikListener。像@Override public View getView(int position, View convertView, ViewGroup parent) { someView.setOnClikListener(new MyOnClickListener(context, itemName)); }
    【解决方案2】:

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

    只需使用 fromPosition 如下,

    公共静态LatLng fromPosition;

    【讨论】:

    • 如果我使用 firebase 数据库来存储 LatLong 数据以及名称等其他数据会怎样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多