检索类或对象的实例

时间:2016-11-01 22:08:52

标签: java android list

我无法理解如何获取对象或类的某些实例。对不起,对于新问题。我有一个名为waypoint的类,其中包含一些关于航点的基本信息,然后我从服务器获取用户最喜欢的航点的列表,然后我检查来自服务器的偏好航路点列表是否与所关注的航点列表相匹配本地设备,如果它没有那么我需要使它们匹配,当试图删除航点的实例我不知道如何删除它的实际实例。我的代码如下:

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.

1 个答案:

答案 0 :(得分:1)

Iterator可能是您的朋友:

    Iterator<Waypoint> iterator = mWaypoint.iterator();
    Waypoint waypoint;
    while (iterator.hasNext()) {
        waypoint = iterator.next();
        if (waypoint.getId() == 1) { // 1 is an example
            iterator.remove();
        }
    }