2017-01-25 2 views
0

Ich habe eine Listview in MainActivity, die OnItemClickListener-Methode hat, die die MapActivity aufruft. Die Kartenaktivität erhält den Breiten- und Längengrad vom CustomAdapter (JSON). Das Ziel ist, wenn man auf eines der Elemente in der Liste klickt, öffnet es die MapActivity und erhält den Breiten- und Längengrad vom CustomAdapter. Mein Problem ist jetzt, dass die MapActivity direkt startet, ohne auf die Elemente zu klicken.Senden von Latlng von CustomAdapter an MapActivity mit Absicht

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

}

Antwort

1

Warum bei Weitergabe von Daten in der Absicht stören, können Sie statische verwenden Variable für den Zugang zu jedem Ort in der Anwendung. Sie können auch Ihre Absicht gemäß Ihren Anforderungen anpassen.

Verwenden Sie einfach from wie unten,

public static LatLng from;

2

Geben Sie die Daten auf diese Weise von CustomListAdapter

erhalten Daten in MapsActivity auf diese Weise

Bundle bundle = getIntent().getExtras(); 

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

Ich möchte die Daten zuerst von CustomAdapter an MainActivity übergeben. Übergeben Sie sie dann an MapActivity von MainActivity – Jason

+0

schreiben Sie SetOnClikListener in getView() -Methode für jede Ansicht. Wie @Override public View getView (int-Position, View convertView, ViewGroup-Parent) { someView.setOnClikListener (neuer MyOnClickListener (context, itemName)); } – Gautam

Verwandte Themen