如何使用POJO(Android)在HashMap中存储和检索多个值

时间:2016-09-30 04:53:23

标签: android firebase firebase-realtime-database google-maps-markers

Map<String, List<Driver>> markerMap = new HashMap<String, List<Driver>>();
private List<Driver> markerArray = new ArrayList<Driver>();
public void markerDriver() {
    markerArray = new ArrayList<>();
    Firebase ref = new Firebase(Config.FIREBASE_URL_DRIVER);
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildrenCount() == 0) {
                markerInfo();
            } else {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Toast.makeText(MainMapActivity.this, "for", Toast.LENGTH_SHORT).show();
                    name = snapshot.child("driversName").getValue().toString().trim();
                    busNum = snapshot.child("busNum").getValue().toString().trim();
                    latitude = Double.valueOf(snapshot.child("latitude").getValue().toString().trim());
                    longitude = Double.valueOf(snapshot.child("longitude").getValue().toString().trim());
                    availableSeat = snapshot.child("availableSeat").getValue().toString().trim();
                    estimatedTime = snapshot.child("estimatedTime").getValue().toString().trim();
                    if ((!latitude.equals(null) || latitude.equals(0)) && (!longitude.equals(null) || longitude.equals(0)) && availableSeat.equals("") && (!estimatedTime.equals("") || estimatedTime.equals("0"))) {
                        convertLatLong();
                        getTotalPass();
                        markerArray.add(new Driver(name, totalPassenger, busNum, latitude, longitude, currentLocation, estimatedTime));
                        markerMap.put("key"+key++, markerArray);
                    }
                }
                for (i = 0; i < markerArray.size(); i++) {
                    createMarker(markerArray.get(i).getDriversName(), markerArray.get(i).getTotalPassenger(), markerArray.get(i).getBusNum(), markerArray.get(i).getLatitude(), markerArray.get(i).getLongitude(), markerArray   .get(i).getLocation(), markerArray.get(i).getEstimatedTime());
                }
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Toast.makeText(MainMapActivity.this, "markerDriver: " + firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

我将我的arrayList放在HashMap中,因为ArrayList本身没有能力编辑/更新列表中的某些数据。所以我要做的是如何检索HashMap和createMarker

中的数据
protected void createMarker(String driversName, final int totalPass, final String busNum, double latitude, double longitude, String location, String estimatedTime) {
    Marker marker = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .title(location)
            .snippet("Driver's Name: " + driversName + "\nTotal Passenger: " + totalPass + "\nBus number: " + busNum+"\nEstimated time: "+estimatedTime));
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
            dialogInfo(marker);
            return true;
        }
    });
}

1 个答案:

答案 0 :(得分:2)

1.首先创建一个像这样的POJO类

public class InterRejectedHistory {

String seeker_role,rejected_date;
String SeekerType;

public InterRejectedHistory(String seeker_role,String seekerType) {
    this.seeker_role = seeker_role;
    this.SeekerType= seekerType;

}


public String getSeekerType() {
    return SeekerType;
}

public void setSeekerType(String seekerType) {
    SeekerType = seekerType;
}


public InterRejectedHistory() {
}


public String getSeeker_role() {
    return seeker_role;
}

public void setSeeker_role(String seeker_role) {
    this.seeker_role = seeker_role;
}

public String getRejected_date() {
    return rejected_date;
}

public void setRejected_date(String rejected_date) {
    this.rejected_date = rejected_date;
}}

2.步骤2 [设定班级中的值]

InterRejectedHistory interRejectedHistory; 
 List<InterRejectedHistory> dbList;  
    dbList=new ArrayList<InterRejectedHistory>();



interRejectedHistory.setSeeker_role(c.getString("SEEKERROLE"));
                        interRejectedHistory.setSeekerType(c.getString("SEEKERTYPE"));
                        dbList.add(new InterRejectedHistory(interRejectedHistory.getSeeker_role(),interRejectedHistory.getSeekerType()));

注意:在Mycase中,我在POJO中设置了值并加载到适配器中。

  1. 无论您想从何处获取POJO类

    的值
    ArrayList<InterRejectedHistory> list;
    
  2. role.setText(list.get(position).getSeeker_role());

    快乐的编码!