1

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..

1 Answers1

1

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.

  • How can I fix my first problem of rewriting the putextra – Atmospheric Productions Mar 07 '20 at 11:22
  • @AtmosphericProductions I expanded my answer a bit. I hope it is clearer now. Keep in mind, my answer is not a good reflection of app architecture. There are many more nuances to this, which you will learn along the way. Focus on the essentials for now :) – Kasper Davidsen Mar 07 '20 at 11:35
  • Then you are better off storing the details in a database like [Room](https://developer.android.com/training/data-storage/room), access it through an API using [Retrofit](https://square.github.io/retrofit/), or even simpler using [String resources](https://developer.android.com/guide/topics/resources/string-resource). Definitely never hard-code it inside your source code for a production-ready app. – Kasper Davidsen Mar 07 '20 at 11:45
  • Becomes too complicated isnt it possible like giving any if statement and only in the if the statement do we mention the strings? – Atmospheric Productions Mar 07 '20 at 11:46
  • You could perhaps use a map, where the key is the name of the country like "India" and the value is a CountryDetail object with a name field such as "India,South Asia" and a description field such as "Funfact about India..." or something. – Kasper Davidsen Mar 07 '20 at 11:50
  • Thankyou alot for this! – Atmospheric Productions Mar 07 '20 at 11:53