-1
我在我的recyclerview上獲取空數據,我使用位置選取器傳遞緯度和經度數據,其正常工作在我的MapActivity上。但是,當我嘗試將這些數據保存在我的NavigationDrawer項活動,它越來越空,如何將緯度和經度數據傳遞到導航抽屜活動
這裏是我的MapActivity
public class MapsActivity extends AppCompatActivity implements
OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayShowHomeEnabled(true);
mMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
try {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
} else {
mMapFragment.getMapAsync(this);
}
} catch (Exception e) {
e.printStackTrace();
}
initialize();
setupDrawer();
}
private void initialize() {
search = (Button) findViewById(R.id.btnSerch);
assert search != null;
search.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnSerch) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
final Double latitude = place.getLatLng().latitude;
final Double longitude = place.getLatLng().longitude;
final String placeName = String.valueOf(place.getName());
final float radius = 0.5f;
LatLng loc2 = new LatLng(latitude, longitude);
marker2 = new MarkerOptions().position(loc2).title(placeName).visible(true);
mMap.addMarker(marker2);
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
}
}
}
private void setupDrawer() {
try {
mDrawer = (NavigationView) findViewById(R.id.mNavDrawer);
assert mDrawer != null;
mDrawer.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.DrawerOpen,
R.string.DrawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Intent intent = null;
if (item.getItemId() == R.id.navigation_item_1) {
mDrawerLayout.closeDrawer(GravityCompat.START);
intent = new Intent(MapsActivity.this, LocationList.class);
startActivity(intent);
return true;
}
if (item.getItemId() == R.id.navigation_item_2) {
mDrawerLayout.closeDrawer(GravityCompat.START);
intent = new Intent(this, AboutUs.class);
startActivity(intent);
return true;
}
if (item.getItemId() == R.id.navigation_item_3) {
mDrawerLayout.closeDrawer(GravityCompat.START);
intent = new Intent(this, Help.class);
startActivity(intent);
return true;
}
return false;
}
的代碼在我LocationList類我有recycleview和IM試圖設置緯度logtitude數據在上面。我怎麼做。
這裏是我的LocationListActivity
public class LocationList extends AppCompatActivity {
private List<Locations> locationsList = new ArrayList<>();
private RecyclerView recyclerView;
private LocationAdapter mAdapter;
int PLACE_PICKER_REQUEST = 1;
String placeName;
Double latitude, longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_list);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new LocationAdapter(locationsList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareLocationData();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
latitude = place.getLatLng().latitude;
longitude = place.getLatLng().longitude;
placeName = String.valueOf(place.getName());
}
}
}
private void prepareLocationData() {
Locations locations = new Locations(placeName, latitude, longitude);
locationsList.add(locations);
mAdapter.notifyDataSetChanged();
}
}
的代碼,當我運行的代碼,它越來越空,我不知道如何來傳遞數據,plz幫助我..這是我的cardview圖像。
ok ..我執行.. –
哇..它的工作,但有一個問題...當我重新啓動我的應用程序,我的cardview再次顯示空。在哪裏我必須使用sharepreference ..?以及如何...... –
是的,您必須將信息存儲在共享首選項中。當您再次打開應用程序時,您需要閱讀共享首選項並設置您的值。 如果您滿意我的答案。請把它解決,並upvote它! 非常感謝:) – GuillaumeAgis