0

how do I get out the individual objects from the getTrails ArrayList below?

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

private static Double myLatitude;
private static Double myLongitude;

Above is declaration of the user location.
Below is where I am dying.

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;

    }


}

I thought I would just have to use CycleTrails.getTrails() followed by the parenthesis that I want, but that does not work. Any help would be sweet

RazerB12
  • 25
  • 7

2 Answers2

0

how do I get out the individual objects from the getTrails ArrayList below?

One way to access the individual objects:

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

another way to access the individual objects:

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

Update

It seems you're having trouble with displaying the result to the console screen. I have implemented a few samples below as requested, hopefully, this will provide you with the base to carry on.

Inside main activity.

 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;
    }

}

NOTE

This data will be displayed on the console screen if you don't know much about that click HERE for further explanation.

Community
  • 1
  • 1
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Cool! So if I went with the first suggestion, and I wanted to pull out the name String, how would I do that? – RazerB12 Mar 09 '17 at 19:04
  • @RazerB12 i will update my answer to show you. and i would really appreciate if you mark the answer as accepted if it has helped you. – Ousmane D. Mar 09 '17 at 19:05
  • @RazerB12 keep me updated if it has worked for you. I have edited my answer – Ousmane D. Mar 09 '17 at 19:09
  • Thanks! it's not quite working, but then I'm not sure if I am implementing it right... at the moment, I just want it to update a textview with a local trail, but it's not functioning. I swapped out the internal bit with the relevant code, but it seems the Textview does not update at all with the local trail. – RazerB12 Mar 09 '17 at 19:17
  • @RazerB12 if you need any help, don't hesitate to post it on StackOverflow, we are all here to help. good luck!. – Ousmane D. Mar 09 '17 at 19:35
  • Cheers Os :) I have a quick question, am I able to call the individual object outside of a For loop? – RazerB12 Mar 09 '17 at 19:58
  • @RazerB12 yes but you cannot refer to the variable declared within the for loop. hence, you may need to do this --> "Trails data1 = CycleTrails.getTrails.get(0);" to get the first element and ---> "Trails data2 = CycleTrails.getTrails.get(1);" to get second element and so forth. but i recommend to use a for loop for safety(IndexOutOfBound errors). – Ousmane D. Mar 09 '17 at 20:05
  • Ahh okay, that's good to know! But the problem still persists in that the name or coords are not being pulled out. Nothing shows up in the logs either. – RazerB12 Mar 09 '17 at 20:33
  • @RazerB12 i will implement a quick algorithm for you to print your data out. – Ousmane D. Mar 09 '17 at 20:38
  • Thank you :) I have declared them inside the CycleTrails class, I forgot to put them back inside when I created this post – RazerB12 Mar 09 '17 at 20:41
  • @RazerB12 put them back and update the post so i can proceed the algorithm for you. – Ousmane D. Mar 09 '17 at 20:42
  • @RazerB12 now "myLatitude" variable is not included.. ;). anyway i can implement it without the "LAT_MIN" and "MyLatitude" etc if you want. but if you want me to include them then please include all the variables within the code. – Ousmane D. Mar 09 '17 at 20:45
  • Haha sorry! I've never had a piece of code made for me before / done collab work. All has been updated, I believe all the variables are now included – RazerB12 Mar 09 '17 at 20:52
  • @RazerB12 I've have implemented it. hopefully it does work for you now and pay attention to the link I've appended below as that will provide you greater details of where to see the result printed and how to print result to console screen. etc. – Ousmane D. Mar 09 '17 at 21:05
  • Thank you so much! Way cleaner. But again, the objects name, lat and lon are not displaying in the logcat :/ weird – RazerB12 Mar 09 '17 at 21:21
  • @RazerB12 unfortunately, I can't think of a way I can further help since the problem persists and I can't see what's actually happening on your screen. Don't give up. keep going. ;). – Ousmane D. Mar 09 '17 at 21:25
  • Thank you anyway :) I think I found the area of problem though... That localTrails list, I don't think it is running...? I put a log inside the if statement, and it never gets printed – RazerB12 Mar 09 '17 at 21:37
  • I have tested the code I implemented for you in my eclipse IDE and it was working fine. – Ousmane D. Mar 09 '17 at 21:38
  • Oh, then that is really weird. Thank you again – RazerB12 Mar 09 '17 at 21:39
  • I FOUND THE PROBLEM, IT'S WORKING, THANK YOU SO MUCH AGAIN – RazerB12 Mar 09 '17 at 21:44
-1

Just use CycleTrails.getTrails.get() and put the specific array position item that you want to get in the parenthesis.