Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 只有一个数组数据时如何从数组中选择数据(自己的数据类)_Java_Android_Arrays - Fatal编程技术网

Java 只有一个数组数据时如何从数组中选择数据(自己的数据类)

Java 只有一个数组数据时如何从数组中选择数据(自己的数据类),java,android,arrays,Java,Android,Arrays,大家好,祝你们在德国一切顺利 数组有问题 我有一个数组,有两组数据,数据1 a,b,c,数据2 d,e,f 现在我想提取d和e,当有信息f时 我的代码: public class MapsActivity extends FragmentActivity { [...] class Data { public Data(int category, String quality, String title,

大家好,祝你们在德国一切顺利

数组有问题

我有一个数组,有两组数据,数据1 a,b,c,数据2 d,e,f

现在我想提取d和e,当有信息f时

我的代码:

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

                }
            }
    );


}

我感谢每一个帮助,即使是简短的.< /p> ,而不是将所有数据实例存储在一个数组中,可以考虑将它们存储在A中,假设您正在使用java。因此,从本质上讲,您的数据实例现在变成:

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.. */
 }

希望这能有所帮助。

请用所用的编程语言标记您的问题。还有三个附加问题:1如何将数据1、数据2等自动存储在HashMap中,例如通过for循环。2 data1.getTitle是什么意思?这意味着要有两个标题1,第一名和第三名?3 data=hashmap.gettitleOfClickedMarker:这到底产生了什么:数据集,例如数据1中有1、***、标题1等?感谢你的评论。我已经更新了我的答案,包括你的新问题1和2。我不知道你的问题3是什么意思。