我想在UserActivity工具栏上找到我的城市名称..我正在MainActivity中搜索并显示我当前的城市名称...我在工具栏上显示了mainactivity cityname但我不知道如何在useractivity中获得该名称...请帮助我...
MainActivity.java
public class MainActivity extends Activity
implements View.OnClickListener {
private LocationManager locationManager=null;
private LocationListener locationListener=null;
private Button btnGetLocation = null;
private EditText editLocation = null;
private ProgressBar pb =null;
String cityName=null;
String addressName = null;
private static final String TAG = "Debug";
private Boolean flag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//if you want to lock screen for always Portrait mode
setRequestedOrientation(ActivityInfo
.SCREEN_ORIENTATION_PORTRAIT);
pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setVisibility(View.INVISIBLE);
editLocation = (EditText) findViewById(R.id.editTextLocation);
btnGetLocation = (Button) findViewById(R.id.btnLocation);
btnGetLocation.setOnClickListener(this);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onClick(View v) {
flag = displayGpsStatus();
if (flag) {
Log.v(TAG, "onClick");
editLocation.setText("Please!! move your device to" +
" see the changes in coordinates." + "\nWait..");
pb.setVisibility(View.VISIBLE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);
} else {
alertbox("Gps Status!!", "Your GPS is: OFF");
}
}
/*----Method to Check GPS is enable or disable ----- */
private Boolean displayGpsStatus() {
ContentResolver contentResolver = getBaseContext()
.getContentResolver();
boolean gpsStatus = Settings.Secure
.isLocationProviderEnabled(contentResolver,
LocationManager.GPS_PROVIDER);
if (gpsStatus) {
return true;
} else {
return false;
}
}
/*----------Method to create an AlertBox ------------- */
protected void alertbox(String title, String mymessage) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your Device's GPS is Disable")
.setCancelable(false)
.setTitle("** Gps Status **")
.setPositiveButton("Gps On",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// finish the current activity
// AlertBoxAdvance.this.finish();
Intent intent =new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
dialog.cancel();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the dialog box
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
/*----------Listener class to get coordinates ------------- */
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
editLocation.setText("");
pb.setVisibility(View.INVISIBLE);
/*----------to get City-Name from coordinates ------------- */
Geocoder gcd = new Geocoder(getBaseContext(),
Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(loc.getLatitude(), loc
.getLongitude(), 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
cityName = addresses.get(0).getLocality();
addressName = addresses.get(0).getSubLocality();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(cityName);
setActionBar(toolbar);
} catch (IOException e) {
e.printStackTrace();
}
//editLocation.setText(longitude+"\n"+latitude + "\n\nMy Current Address is: "+addressName+", "+cityName);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
UserActivity.java
public class UserActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
*toolbar.setTitle("how i should call that cityName here");*
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
您可以使用意图将您的城市名称从MainActivity
传递到UserActivity
。
如果您从UserActivity
开始MainActivity
,请使用putExtra(String, String)
的{{1}}。
例如:
在Intent
MainActivity
并在Intent i = new Intent(MainActivity.this, UserActivity.class);
i.putExtra("Title", yourCityName);
startActivity(i);
UserActivity
或者您可以尝试使用
String cityName = getIntent().getExtra("Title", "Default City Name");
toolbar.setTitle(cityName);
完成后
getSupportActionBar().setTitle(cityName);
答案 1 :(得分:0)
创建一个Constant类。
modal
现在,当您获得城市名称时,请在此处保存此城市名称,如
public class Constant {
public static String CITY_NAME ="";
public static String getCityName() {
return CITY_NAME;
}
public static void setCityName(String cityName) {
Constant.CITY_NAME = cityName;
}
}
如果您想从任何课程或任何活动中检索城市名称,您可以从此处获取此类
Constant.setCityName("ANY");
此名称将在您的应用打开之前存储。所以不需要传递每个活动意图。这样您就可以从应用程序的任何位置获取城市名称。
和您的问题解决方案
MainActivity
Constant.getCityName();
UserActivity
toolbar.setTitle(cityName);
Constant.setCityName(cityName);
setActionBar(toolbar);
将此行更改为
toolbar.setTitle("how i should call that cityName here");
答案 2 :(得分:0)
您可以使用共享偏好设置保存您的城市名称,如下所示:
SharedPreferences sharedpreferences = getSharedPreferences("your preference name", Context.MODE_PRIVATE); Editor editor = sharedpreferences.edit(); editor.putString("cityname", your city name); editor.commit();
现在,只要您想使用您的城市名称:
SharedPreferences sharedpreferences = getSharedPreferences("your preference name", Context.MODE_PRIVATE); String cityname=sharedpreferences.getString("cityname"); toolbar.setTitle(cityname);
答案 3 :(得分:-1)
在MainActivity中将cityName设为静态
public static String cityName=null; //store in this what ever you whant
然后从UserActivity
获取它String city=MainActivity.cityName;