如何调用此ArrayList并获取单个对象?

时间:2017-03-09 01:07:52

标签: java android list object arraylist

如何从下面的getTrails ArrayList中获取单个对象?

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private static Double myLatitude;
private static Double myLongitude;

以上是用户位置的声明 以下是我要死的地方。

static class CycleTrails {

    static class Trails {
        Trails(double lat, double lon, String name){
            this.lat = lat;
            this.lon = lon;
            this.name = name;
        }

        String name;
        double lat;
        double lon;

    }

    static double LAT_MAX = myLatitude + (0.0144927 * 20);
    static double LAT_MIN = myLatitude - (0.0144927 * 20);
    static double LON_MAX = myLongitude + (0.0181818 * 20);
    static double LON_MIN = myLongitude - (0.0181818 * 20);



    public static ArrayList<Trails> getTrails() {
        ArrayList<Trails> trailList = new ArrayList<>();
        Trails t1 = new Trails(51.7181283, -3.3633637, "Bike Park Wales");
        Trails t2 = new Trails(51.6666343, -3.3526367, "Mountain Ash");
        Trails t3 = new Trails(50.5062244,-4.1777985, "FlyUp Downhill");

        trailList.add(t1);
        trailList.add(t2);
        trailList.add(t3);

        ArrayList<Trails> localTrails = new ArrayList<>();
        for(Trails trail : trailList) {
            if (trail.lat > LAT_MIN && trail.lat < LAT_MAX && trail.lon < LON_MIN && trail.lon > LON_MAX) {
                localTrails.add(trail);
                Log.d("Calc", "Run code");


            }

        }

        return localTrails;

    }


}

我以为我只需要使用CycleTrails.getTrails()后跟我想要的括号,但这不起作用。任何帮助都是甜蜜的

2 个答案:

答案 0 :(得分:0)

  

如何从getTrails ArrayList中获取单个对象   下面?

访问单个对象的一种方法:

for(Trails t : CycleTrails.getTrails()){
    //do something with t 
    // Example below:
   Log.d("Name", t.name); // print name's to console screen
}

访问单个对象的另一种方法:

for(int i = 0 ; i < CycleTrails.getTrails().size(); i++){
     CycleTrails temp = CycleTrails.getTrails().get(i);
     // do something with temp
}

<强>更新

您似乎无法将结果显示到控制台屏幕。我按要求在下面实施了一些样本,希望这将为您提供继续进行的基础。

主要活动内部。

 for(Trails t : CycleTrails.getTrails()){
      Log.d("Name", t.name); // print name's to console screen
 }

Trails Class。

public class Trails {
    public  String name;
    public  double lat;   // Object fields
    public  double lon;

    Trails(double lat, double lon, String name){ // construcor
        this.lat = lat;
        this.lon = lon;
        this.name = name;
    }
}

CycleTrails Class

public class CycleTrails {
    static double LAT_MAX = myLatitude + (0.0144927 * 20);
    static double LAT_MIN = myLatitude - (0.0144927 * 20);
    static double LON_MAX = myLongitude + (0.0181818 * 20);
    static double LON_MIN = myLongitude - (0.0181818 * 20);

    public static ArrayList<Trails> getTrails() {
        ArrayList<Trails> trailList = new ArrayList<>();
        Trails t1 = new Trails(51.7181283, -3.3633637, "Bike Park Wales");
        Trails t2 = new Trails(51.6666343, -3.3526367, "Mountain Ash");
        Trails t3 = new Trails(50.5062244,-4.1777985, "FlyUp Downhill");
        trailList.add(t1);
        trailList.add(t2);
        trailList.add(t3);

        ArrayList<Trails> localTrails = new ArrayList<>();
        for(Trails trail : trailList) {
            if (trail.lat > LAT_MIN && trail.lat < LAT_MAX && trail.lon < LON_MIN && trail.lon > LON_MAX) {
               localTrails.add(trail);
               Log.d("Calc", "Run code");
            }
        }
       return localTrails;
    }

}

注意

如果您不太了解该点击HERE以获得进一步说明,则此数据将显示在控制台屏幕上。

答案 1 :(得分:-1)

只需使用CycleTrails.getTrails.get()并将要获取的特定数组位置项放在括号中。