2017-01-25 29 views
0

我在MainActivity中有一個ListView,它具有調用MapActivity的OnItemClickListener方法。地圖活動從CustomAdapter(JSON)獲取緯度和經度。目標是當點擊列表中的某個項目時,它將打開MapActivity並從CustomAdapter獲取經度和緯度。我現在的問題是,MapActivity直接啓動而不點擊項目。使用intent從CustomAdapter發送Latlng到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)); 
} 

}

回答

1

爲什麼與數據傳遞到懶得意圖,可以在應用程序中使用靜態變量的訪問任何地方。你也可以根據你的要求調整你的意圖。

只需使用fromPosition如下,

public static LatLng fromPosition;

2

從CustomListAdapter

傳遞數據這樣0
@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"); 
+0

我想先將數據從CustomAdapter傳遞給MainActivity。然後將它們從MainActivity – Jason

+0

傳遞給MapActivity,在任何視圖的getView()方法中寫setOnClikListener。 Like @Override public View getView(int position,View convertView,ViewGroup parent){ someView.setOnClikListener(new MyOnClickListener(context,itemName)); } – Gautam

相關問題