2016-09-30 37 views
0
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如何存儲和檢索HashMap中使用POJO(安卓)多個值

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

回答

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.Step 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中的值並將其加載到適配器中。

  • 無論你想從POJO類中獲得的價值

    ArrayList<InterRejectedHistory> list; 
    
  • role.setText(list.get(position).getSeeker_role());

    編碼快樂!