我如何为某些意图转移不同的 .putextras?
How can I transfer different .putextras for some intent?
所以我有一些标记和自定义信息 Windows 和 putextras。
我不知道如何使用 putextras,但我知道如何使用它们。
所以我使用了一个 put extra 将数据从 MapsActivity 传输到 Country Adapter,我将对此进行解释:
所以我的想法是,当用户单击 CustomInfoWindow 时,将打开一个名为 Country Adapter 的意图。该国家/地区适配器有 2 个标题和内容文本视图。目前我只成功地传输了一组数据,但另一组数据不起作用。我可能做错了什么,但我在设置 Put extras 时真的很困惑。
地图活动:
@Override
public void onInfoWindowClick(Marker marker) {
if("India".equals(marker.getTitle())) {
Intent intent = new Intent(this,Country.class);
intent.putExtra("India","text");
startActivity(intent);
}
if("Australia".equals(marker.getTitle())){
Intent intent = new Intent(this,Country.class);
intent.putExtra("Austrailia","text");
// want to create a method that when this is clicked it gives different texts.
startActivity(intent);
}
我的国家/地区适配器:
Button bt = findViewById(R.id.button);
bt.setOnClickListener(v -> openMap());
TextView countryName = findViewById(R.id.textView);
TextView Main = findViewById(R.id.textView2);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String India = bundle.getString("India");
countryName.setText("India,South Asia");
}
if(bundle != null) {
String Australia = bundle.getString("Australia");
countryName.setText("Australia,Oceania");
}
}
public void openMap(){
finish();
}
}
我想为它们显示不同的文本,但它不起作用。我对此很陌生,所以请详细回答..
您的方向是正确的,但您的主要问题在于您从 Intent 包中提取数据的方式。
首先,考虑您的代码:
if(bundle != null) {
String India = bundle.getString("India");
countryName.setText("India,South Asia");
}
if(bundle != null) {
String Australia = bundle.getString("Australia");
countryName.setText("Australia,Oceania");
}
您基本上首先提取有关印度的信息。不管这是否成功,您都可以为澳大利亚做同样的事情——覆盖您刚刚为印度设置的文本。您将永远只能看到澳大利亚作为输出。
你的第二个问题在于你使用Intent extras的方式。来自 Android documentation:
public Intent putExtra (String name, String value)
name String: The name of the extra data, with package prefix.
value String: The String data value. This value may be null.
所以你最好使用额外的名称,例如 com.mypackage.CountryName
,然后将值设置为 India
。然后,您可以有一个名为 com.mypackage.CountryDetails
的单独 Intent extra,其中包含您希望显示的描述性值。在您的 MapsActivity 中:
Intent intent = new Intent(this,Country.class);
if("India".equals(marker.getTitle())) {
intent.putExtra("com.mypackage.CountryName", "India,South Asia");
intent.putExtra("com.mypackage.CountryDetails", "...");
} else if("Australia".equals(marker.getTitle())){
intent.putExtra("com.mypackage.CountryName", "Australia,Oceania");
intent.putExtra("com.mypackage.CountryDetails", "...");
}
startActivity(intent);
在您的国家/地区适配器中:
Button bt = findViewById(R.id.button);
bt.setOnClickListener(v -> openMap());
TextView countryName = findViewById(R.id.textView);
TextView Main = findViewById(R.id.textView2);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String name = bundle.getString("com.mypackage.CountryName");
countryName.setText(name);
String details = bundle.getString("com.mypackage.CountryDetails");
countryDetails.setText(details);
// If you absolutely must do something on a per-country basis, then use a
// switch or chained if..else inside of this if statement
}
// Notice how the second if statement is gone
}
public void openMap(){
finish();
}
}
大家可以就职责委派和架构进行长篇大论。我暂时忽略这个。但就命名 Intent extras 的良好编码实践而言,请查看 this related answer。
所以我有一些标记和自定义信息 Windows 和 putextras。 我不知道如何使用 putextras,但我知道如何使用它们。 所以我使用了一个 put extra 将数据从 MapsActivity 传输到 Country Adapter,我将对此进行解释: 所以我的想法是,当用户单击 CustomInfoWindow 时,将打开一个名为 Country Adapter 的意图。该国家/地区适配器有 2 个标题和内容文本视图。目前我只成功地传输了一组数据,但另一组数据不起作用。我可能做错了什么,但我在设置 Put extras 时真的很困惑。 地图活动:
@Override
public void onInfoWindowClick(Marker marker) {
if("India".equals(marker.getTitle())) {
Intent intent = new Intent(this,Country.class);
intent.putExtra("India","text");
startActivity(intent);
}
if("Australia".equals(marker.getTitle())){
Intent intent = new Intent(this,Country.class);
intent.putExtra("Austrailia","text");
// want to create a method that when this is clicked it gives different texts.
startActivity(intent);
}
我的国家/地区适配器:
Button bt = findViewById(R.id.button);
bt.setOnClickListener(v -> openMap());
TextView countryName = findViewById(R.id.textView);
TextView Main = findViewById(R.id.textView2);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String India = bundle.getString("India");
countryName.setText("India,South Asia");
}
if(bundle != null) {
String Australia = bundle.getString("Australia");
countryName.setText("Australia,Oceania");
}
}
public void openMap(){
finish();
}
}
我想为它们显示不同的文本,但它不起作用。我对此很陌生,所以请详细回答..
您的方向是正确的,但您的主要问题在于您从 Intent 包中提取数据的方式。
首先,考虑您的代码:
if(bundle != null) {
String India = bundle.getString("India");
countryName.setText("India,South Asia");
}
if(bundle != null) {
String Australia = bundle.getString("Australia");
countryName.setText("Australia,Oceania");
}
您基本上首先提取有关印度的信息。不管这是否成功,您都可以为澳大利亚做同样的事情——覆盖您刚刚为印度设置的文本。您将永远只能看到澳大利亚作为输出。
你的第二个问题在于你使用Intent extras的方式。来自 Android documentation:
public Intent putExtra (String name, String value)
name String: The name of the extra data, with package prefix.
value String: The String data value. This value may be null.
所以你最好使用额外的名称,例如 com.mypackage.CountryName
,然后将值设置为 India
。然后,您可以有一个名为 com.mypackage.CountryDetails
的单独 Intent extra,其中包含您希望显示的描述性值。在您的 MapsActivity 中:
Intent intent = new Intent(this,Country.class);
if("India".equals(marker.getTitle())) {
intent.putExtra("com.mypackage.CountryName", "India,South Asia");
intent.putExtra("com.mypackage.CountryDetails", "...");
} else if("Australia".equals(marker.getTitle())){
intent.putExtra("com.mypackage.CountryName", "Australia,Oceania");
intent.putExtra("com.mypackage.CountryDetails", "...");
}
startActivity(intent);
在您的国家/地区适配器中:
Button bt = findViewById(R.id.button);
bt.setOnClickListener(v -> openMap());
TextView countryName = findViewById(R.id.textView);
TextView Main = findViewById(R.id.textView2);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String name = bundle.getString("com.mypackage.CountryName");
countryName.setText(name);
String details = bundle.getString("com.mypackage.CountryDetails");
countryDetails.setText(details);
// If you absolutely must do something on a per-country basis, then use a
// switch or chained if..else inside of this if statement
}
// Notice how the second if statement is gone
}
public void openMap(){
finish();
}
}
大家可以就职责委派和架构进行长篇大论。我暂时忽略这个。但就命名 Intent extras 的良好编码实践而言,请查看 this related answer。