MapActivityを呼び出すOnItemClickListenerメソッドを持つMainActivityのリストビューがあります。マップアクティビティは、CustomAdapter(JSON)から緯度と経度を取得します。目標は、リスト内の項目の1つをクリックしてMapActivityを開き、CustomAdapterから緯度と経度を取得することです。私の問題は、MapActivityがアイテムをクリックせずに直接起動することです。インテントを使用してCustomAdapterからMapActivityにLatlngを送信する
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));
}
}
まず、CustomAdapterのMainActivityにデータを渡します。次に、どのビューでもgetActivity – Jason
からgetActivityにgetView()メソッドのsetOnClikListenerを書き込みます。 @Overrideのように publicビューgetView(int position、ビューconvertView、ViewGroup親){ someView.setOnClikListener(new MyOnClickListener(context、itemName)); } – Gautam