0

Hi and all the best from Germany,

having a problem with an array.

I have an array with 2 sets of data (data 1 (a, b, c), data 2 (d, e, f)).

Now I want to extract "d" and "e", when having the Information "f".

My code:

public class MapsActivity extends FragmentActivity {

[...]
class Data {
    public Data(int category,
                String quality,
                String title,
                String snippet,
                float lng,
                float lat,
                String homepage,
                String phonenumber,
                float distance,
                float duration

               )
    {
        super();
        this.category = category;
        this.quality = quality;
        this.title = title;
        this.snippet = snippet;
        this.lng = (float)lng;
        this.lat = (float)lat;
        this.homepage = homepage;
        this.phonenumber = phonenumber;
        this.distance = (float) distance;
        this.duration = (float) duration;

    }
    int category;
    String quality;
    String title;
    String snippet;
    float lng;
    float lat;
    String homepage;
    String phonenumber;
    float distance;
    float duration;

}

Data[] data = {
        new Data(1,
                "***",
                "title1",
                "snippet1",
                0.0f,
                0.0f,
                "http://www.something.something",
                "tel:+00000000",
                0,0),

        new Data(1,
                "***",
                "title2",
                "snippet2",
                0.0f,
                0.0f,
                "http://www.something.something",
                "tel:+00000000",
                0,0),
/**
        new Data(1, -79.402206f,43.657688f, "College St",
                "Lots of discount computer stores if you forgot a cable or need to buy hardware."),

        new Data(1, -79.390381f,43.659878f, "Queens Park Subway",
                "Quickest way to the north-south (Yonge-University-Spadina) subway/metro line"),

        new Data(1, -79.403732f,43.666801f, "Spadina Subway",
                "Quickest way to the east-west (Bloor-Danforth) subway/metro line"),

        new Data(1, -79.399696f,43.667873f, "St George Subway back door",
                "Token-only admittance, else use Spadina or Bedford entrances!"),

        new Data(1, -79.384163f,43.655083f, "Eaton Centre (megamall)",
                "One of the largest indoor shopping centres in eastern Canada. Runs from Dundas to Queen."),
 */
};



@Override
protected void onCreate(Bundle savedInstanceState) {
[...]

    // 8. when the infowindow ist clicked
    myMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    String titleOfClickedMarker = marker.getTitle();

                    Toast.makeText(getApplicationContext(),titleOfClickedMarker, Toast.LENGTH_SHORT).show();

// HERE I NEED HELP: I now have the title (for example title 1), and now the Point: HOW DO I GET THE OTHER INFORMATION OF THE "TITLE1" data set (e.g. snippet 1, etc.). 

                }
            }
    );


}

I appreciate every help, even if short.

1
  • 1
    Please tag your question with the programming language used. Commented Oct 16, 2014 at 17:54

1 Answer 1

1

Instead of storing all your Data instances in an array, you can consider storing them in a HashMap, assuming you are working with Java. So essentially, your data instance now becomes:

class Data{
  /* etc... */
  public String getText(){
    return this.title;
  }
}

Data data1 = new Data(1, "***", "title1", "snippet1",
                      0.0f, 0.0f, "http://www.something.something",
                      "tel:+00000000", 0,0);

Data data2 = new Data(1, "***", "title2", "snippet2",
                      0.0f, 0.0f, "http://www.something.something",
                      "tel:+00000000",
                      0,0);
/* etc... */

HashMap<String, Data> hashMap = new HashMap<String, Data>();
for(int i = 0; i < dataCount; i++) // add data instances in a loop. Define your own dataCount
  hashMap.add(data.getTitle(), data);

/* etc... */

myMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
  @Override
  public void onInfoWindowClick(Marker marker) {
    String titleOfClickedMarker = marker.getTitle();
    Data data = hashMap.get(titleOfClickedMarker);
    /* etc.. */
 }

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

There are three addional questions: (1) How can "data1", "data2", etc. be stored in a HashMap automatically (e.g. via for-Loop). (2) What does "data1.getTitle()" mean? Is this meant to have the "title1" twice, at first place and on third? (3) The "data = hashmap.get(titleOfClickedMarker)": What exactly does this produce: the set of the data, e.g. data1 (with "1", "***", "title1", etc. in it)? Appriciate your comment.
I have updated my answer to include your new questions (1) and (2). I am not sure what your question (3) meant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.