如何将活动中的ListView中的数据传递给fragment要查看地图上的位置,请看我在FragmentTwoClass中的方法addMarker()? ................................................... ...................................................
FavoritesActivity:
公共类收藏夹扩展了AppCompatActivity {
private ArrayList<MapModel> mMapList; // ArrayList of MovieModel
private MapCustomAdapterFavorites mAdapter; // CustomAdapter of MainActivity
private GetMapsAsyncTaskFavorites mGetMapsAsyncTaskFavorites; // AsyncTask for AddMovie to add movie to MainActivity
private ListView mListView; // ListView of MainActivity
private MapDBHelperFavorites mMapDBHelper; // The SQLiteHelper of the app
private SwipeRefreshLayout swipeRefreshLayout; // SwipeRe freshLayout of MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("LocationProject");
mListView = findViewById(R.id.listFavorites); // ID of the ListView of MainActivity
swipeRefreshLayout = findViewById(R.id.swipe_container); // ID of the SwipeRefreshLayout of MainActivity
mMapDBHelper = new MapDBHelperFavorites(this); // Put the SQLiteHelper in MainActivity
mMapList = mMapDBHelper.getAllMaps(); // Put the getAllMovies of SQLiteHelper in the ArrayList of MainActivity
mAdapter = new MapCustomAdapterFavorites(this, mMapList); // Comparing the ArrayList of MainActivity to the CustomAdapter
registerForContextMenu(mListView);
// Put AsyncTask in the ListView of MainActivity to execute the SQLiteHelper
mGetMapsAsyncTaskFavorites = new GetMapsAsyncTaskFavorites(mListView);
mGetMapsAsyncTaskFavorites.execute(mMapDBHelper);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTwo fragObj = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putSerializable(getString(R.string.map_edit_favorites), (mMapList.get(position)));
fragObj.setArguments(bundle);
}
});
swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorOrange)); // Colors of the SwipeRefreshLayout of MainActivity
// Refresh the MovieDBHelper of app in ListView of MainActivity
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mGetMapsAsyncTaskFavorites = new GetMapsAsyncTaskFavorites(mListView);
mGetMapsAsyncTaskFavorites.execute(mMapDBHelper);
// Vibration for 0.1 second
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(100);
}
finish();
startActivity(getIntent()); // Refresh activity
Toast toast = Toast.makeText(Favorites.this, "The list are refreshed!", Toast.LENGTH_SHORT);
View view = toast.getView();
view.getBackground().setColorFilter(getResources().getColor(R.color.colorLightBlue), PorterDuff.Mode.SRC_IN);
TextView text = view.findViewById(android.R.id.message);
text.setTextColor(getResources().getColor(R.color.colorBrown));
toast.show(); // Toast
swipeRefreshLayout.setRefreshing(false);
}
});
}
// Sets off the menu of activity_menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.favorites_menu, menu);
return super.onCreateOptionsMenu(menu);
}
// Sets off the menu of list_menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.favorites_list_menu, menu);
}
// Options in the activity_menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mute: // Mute all the sound in app
AudioManager managerYes = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
managerYes.setStreamMute(AudioManager.STREAM_MUSIC, true);
Toast toastMute = Toast.makeText(this, "The sound are mute!", Toast.LENGTH_SHORT);
View viewMute = toastMute.getView();
viewMute.getBackground().setColorFilter(getResources().getColor(R.color.colorLightBlue), PorterDuff.Mode.SRC_IN);
TextView textMute = viewMute.findViewById(android.R.id.message);
textMute.setTextColor(getResources().getColor(R.color.colorBrown));
toastMute.show(); // Toast
break;
case R.id.unMute: // Allow all the sound in app
AudioManager managerNo = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
managerNo.setStreamMute(AudioManager.STREAM_MUSIC, false);
Toast toastUnMute = Toast.makeText(this, "The sound are on!", Toast.LENGTH_SHORT);
View viewUnMute = toastUnMute.getView();
viewUnMute.getBackground().setColorFilter(getResources().getColor(R.color.colorLightBlue), PorterDuff.Mode.SRC_IN);
TextView textUnMute = viewUnMute.findViewById(android.R.id.message);
textUnMute.setTextColor(getResources().getColor(R.color.colorBrown));
toastUnMute.show(); // Toast
break;
case R.id.intentMainActivity:
Intent intentBackMainActivity = new Intent(this, MainActivity.class);
startActivity(intentBackMainActivity);
break;
case R.id.deleteAllDataFavorites: // Delete all data of the app for delete all the data of the app
Intent intentDeleteAllData = new Intent(this, DeleteAllDataFavorites.class);
startActivity(intentDeleteAllData);
break;
}
return super.onOptionsItemSelected(item);
}
// Options in the list_menu
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int listPosition = info.position;
switch (item.getItemId()) {
case R.id.edit: // Edit the movies on MainActivity
Intent intent = new Intent(Favorites.this, EditMap.class);
intent.putExtra(getString(R.string.map_id), mMapList.get(listPosition).getId());
intent.putExtra(getString(R.string.map_edit), mMapList.get(listPosition));
startActivity(intent);
break;
case R.id.shareIntent:
String name = mMapList.get(listPosition).getName();
String address = mMapList.get(listPosition).getVicinity();
double lat = mMapList.get(listPosition).getLat();
double lng = mMapList.get(listPosition).getLng();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Name: " + name + "\nAddress: " + address + "\nLatitude: " + lat + "\nLongitude: " + lng);
sendIntent.setType("text/plain");
startActivity(sendIntent);
break;
case R.id.delete: // Delete item(movie) on MainActivity
mGetMapsAsyncTaskFavorites.deleteMovie(listPosition);
mMapDBHelper.deleteMap(mMapList.get(listPosition));
Intent intentDeleteData = new Intent(Favorites.this, DeleteMap.class);
startActivity(intentDeleteData);
break;
}
return super.onContextItemSelected(item);
}
FragmentTwoClass:
公共类FragmentTwo扩展Fragment实现OnMapReadyCallback {
private GoogleMap mGoogleMap;
private MapView mMapView;
private View mView;
private MapModel mapModel;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_two_layout, container, false);
mapModel = (MapModel) getArguments().getSerializable(getString(R.string.map_edit_favorites)); // GetSerializable for the texts
return mView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = view.findViewById(R.id.map);
if (mMapView != null) {
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
addMarker();
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
}
public void addMarker() {
MarkerOptions marker = new MarkerOptions().position(new LatLng(mapModel.getLat(), mapModel.getLng())).title("Elior home");
mGoogleMap.addMarker(marker);
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return true;
}
});
}