从 alertDialog 获取选定值并使用选定值在 android 中重新加载 activity
Get selected value from alertDialog and reload activity in android using the selected value
我正在开发一个 android 应用程序,它获取用户的位置并在地图上的特定半径内绘制他周围的点。半径被硬编码为 30 英里。我使用这个半径对我的服务器进行 API 调用,它给我要绘制的点。因此,这里的半径可以是一个字符串(如“10”、“15”、“20”等)
我正在尝试为其添加设置功能,以便用户可以更改半径。我正在实施一个 alertDialog,我将在其中显示各种半径选项。
如果用户从可用半径中选择一个半径,应用程序应使用所选半径重新加载。以下是代码。
我面临的问题是 activity 没有重新加载。该应用程序运行良好,没有任何错误,但是当我转到设置并单击半径时,它不会重新加载 activity。警报对话框关闭,但没有任何反应。
注意:我已经从原始代码中删除了声明的变量和其他函数,只写下了不工作的部分。
我尝试调用 onCreate 方法,但它没有用。
我尝试使用 intent 重新加载 activity,如下所示,但即使那样也没有用。
//This class implements OnMyLocationChangeListener that is used to
//display the user's current location on the map
public class HotspotScreen extends ActionBarActivity implements OnMyLocationChangeListener{
//varibles of the application declared here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotspot_screen);
// I plan to reload the activity by sending the radius string to the activity
// and then use it to load the activity using the selected radius
// following is to see if radius is sent while reloading the activity
//if yes, using the radius selected.
Intent intent = getIntent();
if (intent.getStringExtra("start") != null){
radius = intent.getStringExtra("start");
}
Toast.makeText(this, "Radius: "+radius, Toast.LENGTH_LONG).show();
//this is my function to setup the map
setUpMapIfNeeded();
}
// following is the code to pop up the alertdialog which gives a list of
// values for radius for the user to choose.
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.hotspot_screen_settings, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.settings:
settingsMenuItem();
break;
}
return true;
}
private void settingsMenuItem() {
String[] radius_list = {"10", "15", "20", "25", "30", "35", "40", "45" ,"50"};
new AlertDialog.Builder(this)
.setTitle("Change radius")
.setItems(radius_list, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch(which){
case 10:
Intent i = new Intent(HotspotScreen.this, HotspotScreen.class);
i.putExtra("radius", "10");
startActivityForResult(i, 0);
HotspotScreen.this.finish();
//HotspotScreen.this.onCreate(null);
break;
case 15:
Intent i = new Intent(HotspotScreen.this, HotspotScreen.class);
i.putExtra("radius", "15");
startActivityForResult(i, 0);
HotspotScreen.this.finish();
//HotspotScreen.this.onCreate(null);
break;
case 20:
Intent i = new Intent(HotspotScreen.this, HotspotScreen.class);
i.putExtra("radius", "20");
startActivityForResult(i, 0);
HotspotScreen.this.finish();
//HotspotScreen.this.onCreate(null);
break;
}
}
})
.show();
}
提前致谢:)
问题是,onClick方法中的which
值并不是你的半径值,它是被点击的项目的位置。所以尝试:
switch(radius_list[which]){
case "10":...
case "15":...
}
而且我个人认为您不需要为这种情况重新加载 activity,您只需在开关块中设置半径并再次调用 setUpIfNeeded 来更改视图即可。
我正在开发一个 android 应用程序,它获取用户的位置并在地图上的特定半径内绘制他周围的点。半径被硬编码为 30 英里。我使用这个半径对我的服务器进行 API 调用,它给我要绘制的点。因此,这里的半径可以是一个字符串(如“10”、“15”、“20”等)
我正在尝试为其添加设置功能,以便用户可以更改半径。我正在实施一个 alertDialog,我将在其中显示各种半径选项。
如果用户从可用半径中选择一个半径,应用程序应使用所选半径重新加载。以下是代码。
我面临的问题是 activity 没有重新加载。该应用程序运行良好,没有任何错误,但是当我转到设置并单击半径时,它不会重新加载 activity。警报对话框关闭,但没有任何反应。
注意:我已经从原始代码中删除了声明的变量和其他函数,只写下了不工作的部分。 我尝试调用 onCreate 方法,但它没有用。 我尝试使用 intent 重新加载 activity,如下所示,但即使那样也没有用。
//This class implements OnMyLocationChangeListener that is used to
//display the user's current location on the map
public class HotspotScreen extends ActionBarActivity implements OnMyLocationChangeListener{
//varibles of the application declared here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotspot_screen);
// I plan to reload the activity by sending the radius string to the activity
// and then use it to load the activity using the selected radius
// following is to see if radius is sent while reloading the activity
//if yes, using the radius selected.
Intent intent = getIntent();
if (intent.getStringExtra("start") != null){
radius = intent.getStringExtra("start");
}
Toast.makeText(this, "Radius: "+radius, Toast.LENGTH_LONG).show();
//this is my function to setup the map
setUpMapIfNeeded();
}
// following is the code to pop up the alertdialog which gives a list of
// values for radius for the user to choose.
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.hotspot_screen_settings, menu);
return super.onCreateOptionsMenu(menu);
//return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.settings:
settingsMenuItem();
break;
}
return true;
}
private void settingsMenuItem() {
String[] radius_list = {"10", "15", "20", "25", "30", "35", "40", "45" ,"50"};
new AlertDialog.Builder(this)
.setTitle("Change radius")
.setItems(radius_list, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch(which){
case 10:
Intent i = new Intent(HotspotScreen.this, HotspotScreen.class);
i.putExtra("radius", "10");
startActivityForResult(i, 0);
HotspotScreen.this.finish();
//HotspotScreen.this.onCreate(null);
break;
case 15:
Intent i = new Intent(HotspotScreen.this, HotspotScreen.class);
i.putExtra("radius", "15");
startActivityForResult(i, 0);
HotspotScreen.this.finish();
//HotspotScreen.this.onCreate(null);
break;
case 20:
Intent i = new Intent(HotspotScreen.this, HotspotScreen.class);
i.putExtra("radius", "20");
startActivityForResult(i, 0);
HotspotScreen.this.finish();
//HotspotScreen.this.onCreate(null);
break;
}
}
})
.show();
}
提前致谢:)
问题是,onClick方法中的which
值并不是你的半径值,它是被点击的项目的位置。所以尝试:
switch(radius_list[which]){
case "10":...
case "15":...
}
而且我个人认为您不需要为这种情况重新加载 activity,您只需在开关块中设置半径并再次调用 setUpIfNeeded 来更改视图即可。