Android/Java開發相當新穎,並且使用開源的Parseplatform作爲我的後端服務器。我已經創建了一個類來管理一個解析對象,並且此對象根據此代碼將其數據從異步調用更新爲解析服務器。來自異步類的多重嵌套回調
public class DeviceObject {
private String objectID, deviceName, status;
private ParseGeoPoint location;
int batLevel;
public DeviceObject(){
objectID = null;
deviceName = null;
location = null;
batLevel = 0;
status = null;
}
public void getDeviceLatestData() {
if (objectID != null) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("DeviceData");
query.whereEqualTo("DeviceObjectID", objectID);
query.orderByDescending("createdAt");
query.setLimit(1);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> ParseDeviceList, ParseException e) {
if (e == null) {
if (ParseDeviceList.size() == 0) {
Log.d("debg", "Device not found");
} else {
for (ParseObject ParseDevice : ParseDeviceList) {
status = ParseDevice.getString("Status");
batLevel = ParseDevice.getInt("BatteryLevel");
location = ParseDevice.getParseGeoPoint("Location");
Log.d("debg", "Retrieving: " + deviceName);
Log.d("debg", "Status: " + status + " Battery: " + Integer.toString(batLevel));
}
//callback listener to add marker to map
}
} else {
Log.d("debg", "Error: " + e.getMessage());
}
}
});
}
}
所以我創建我的類對象在我的主要活動有以下幾點:
DeviceObject userDevice = new DeviceObject();
userDevice.getDeviceLatestData();
我可以不在身邊讓我的頭是怎麼在我的MainActivity我可以得到通知/回調繼續顯示userDevice類剛剛從解析服務器下載的信息。
我試圖創建一個接口,並添加監聽,因爲我已經看到了建議,但是我不能添加解析的完成函數內部的聽衆是什麼。
我的主要活動的定義,請注意我需要OnMapReadyCallback我使用谷歌地圖
public class MapMainActivity extends AppCompatActivity implements OnMapReadyCallback {
因此,在總結,我想添加一些主要的活動,這樣我可以處理我數據從異步調用中添加到類中時的數據。
哇那工作,那麼容易。直到今天,我從來沒有聽說過事件總線,它工作得很好。非常感謝! – BrentonGray88
@ BrentonGray88當然! – kpsharp