0

I am having trouble understanding how to get certain instances of an object or class. Sorry for the newb question. I have a class called waypoint which contains some basic info about the waypoint, next im grabbing a list of the user's favorited waypoints from a server, then i check to see if the list of favorited waypoints from the server matches the list of favorited waypoints on the local device if it doesnt then i need to make them match and when trying to remove a instance of the waypoint I dont know how to remove the actual instance of it. My code is below:

waypoint.java

public class Waypoint
{
  public long id;
  public String name;
  public Bitmap bitmap;
  public boolean deleted;
  public int waypoint_type;
  public int waypoint_id;

  public Waypoint(long id, String name, Bitmap bitmap, int waypoint_type, int waypoint_id)
  {
    this.id = id;
    this.name = name;
    this.bitmap = bitmap;
    this.waypoint_type = waypoint_type;
    this.waypoint_id = waypoint_id;
  }
  public long getId() {
  return id;
  }
}

MainActivity.java

public ArrayList<Waypoint> mWaypoint = new ArrayList<>();
mWaypoint .add(new Waypoint(getNewId(), name, BitmapFactory.decodeResource(getResources(), R.drawable.stoplogo_small), stopTypeInteger, idInteger));

//attempt to remove it from my live list view
int pos = mAdapterWaypoint.getPosition(WaypointObjectGoesHere); //I dont know how to get this instance that I need here.
mAdapterWaypoint.remove(WaypointObjectGoesHere); //I dont know how to get this instance that I need here.
rapid3642
  • 913
  • 2
  • 14
  • 26

1 Answers1

1

An Iterator may be your friend here:

    Iterator<Waypoint> iterator = mWaypoint.iterator();
    Waypoint waypoint;
    while (iterator.hasNext()) {
        waypoint = iterator.next();
        if (waypoint.getId() == 1) { // 1 is an example
            iterator.remove();
        }
    }
chris g
  • 1,088
  • 10
  • 18
  • Sorry im really new to all of this. I noticed there is a getId() function call, would you be able to provide an example of how such a function would look like? – rapid3642 Nov 02 '16 at 01:15
  • 1
    Sure. In your Waypoint class, you'd create a 'getter' as such: `public long getId() { return id; }` – chris g Nov 02 '16 at 05:03
  • OHHHH! ok I remember going over getters and setters from a class I took. thanks! that definitely helps! However I went ahead and created the getter like you wrote but for whatever reason iterator.getId() is still showing as "cannot resolve method". Any ideas why this is? Is it because the array that I'm iterating doesn't have the class getId()? I'm a little lost. Sorry for all the confusion. – rapid3642 Nov 02 '16 at 22:32
  • 1
    Apologies, I didn't know your experience level. here, I'll change my example above. – chris g Nov 03 '16 at 01:06
  • Thank you soooo much for working with me on this! I know its a pain to work with newbs sometimes but thank you very much for taking the time to help me through my problem, this worked perfectly! Answer marked as accepted! I would like to pick your brain a little more though however, what would be the benefit over using a getter or setter rather than just doing .id for the waypoint? Again thank you so much for taking the time to help me. – rapid3642 Nov 03 '16 at 06:01
  • You are *very* welcome! Someone wrote up an excellent set of reasons for using getters and setters here: http://stackoverflow.com/questions/1568091/why-use-getters-and-setters it's great information, especially since you're new. Please feel free to PM me any time with questions. You will also find the rest of this community to be very helpful and responsive. – chris g Nov 03 '16 at 06:49