How can I transfer different .putextras for some intent?

Atmospheric Productions :

So i have some markers and Custom Info Windows and putextras. I don't know to use putextras that much but I know how to use them a bit. So I used a put extra to transfer data from MapsActivity to Country Adapter which I will explain: So my idea is that when a user clicks on CustomInfoWindow an intent opens named Country Adapter. That country adapter has 2 textviews for title and content. Currently i am successful just in transferring one set of data but the other just doesn't work. I may be doing something wrong but I get really confused in setting Put extras. MapsActivity:

@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);
        }

My Country Adapter:

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();
    }
}

I want to display different texts for both of them but it doesn't work. I am very new to this so please answer in detail..

Kasper Davidsen :

You are on the right track, but your primary issue lies in the way you extract the data from the Intent bundle.

Firstly, consider your code:

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");
}

You essentially first extract information about India. Regardless of whether this is successful, you then do the same for Australia - overwriting the text you just set for India. You will only ever be able to see Australia as the output.

Your second problem lies in the way you use Intent extras. From the 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.

So you are better off using a name of the extra such as com.mypackage.CountryName and then set the value to India. You can then have a separate Intent extra named com.mypackage.CountryDetails with the descriptive value you wish to show. In your 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);

And in your Country Adapter:

        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();
    }
}

You can have a long discussion about delegation of responsibilities and architecture. I ignore this for now. But in terms of good coding practices for naming your Intent extras, have a look at this related answer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31368&siteId=1