java - 当只有 1 条数组数据(自己的数据类)时,如何从数组中选择数据

标签 java android arrays

您好,祝德国一切顺利,

数组有问题。

我有一个包含 2 组数据的数组(数据 1 (a, b, c),数据 2 (d, e, f))。

现在我想在拥有信息“f”时提取“d”和“e”。

我的代码:

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

                }
            }
    );


}

我感谢每一个帮助,即使是短暂的。

最佳答案

您可以考虑将它们存储在 HashMap 中,而不是将所有 Data 实例存储在数组中。 ,假设您正在使用 Java。所以本质上,您的 data 实例现在变成:

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

希望这有帮助。

关于java - 当只有 1 条数组数据(自己的数据类)时,如何从数组中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26410767/

相关文章:

java - 这种基于 Java 的多密码加密格式实现安全吗?

java - Android - AsyncTask 上的 RuntimeException

使用 memcpy 将一维数组复制到一行二维数组

javascript - 将值添加到数组中的对象

java - 最近一直在看很多第三方库代码,看到这段代码让我很困惑

java - 从 Spring MVC 中的 Controller 操作重定向到外部 URL

java - 循环方法

android - 数据未显示在 ListView 中

android - 如何将应用程序本地存储同步到数据库?

c - 在 C 编程中反转前 n 个数字的数组