Android / Java开发相当新,并使用Open Source 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类刚刚从解析服务器上获取的信息。
我已经尝试创建一个界面并添加一个听众,就像我所看到的那样,但是我无法在解析的done函数中添加监听器。
我的主要活动的定义是,注意我需要使用OnMapReadyCallback,因为我正在使用谷歌地图
public class MapMainActivity extends AppCompatActivity implements OnMapReadyCallback {
总而言之,我想在主要活动中添加一些内容,这样我就可以在从异步调用中将数据添加到类中时处理数据。
答案 0 :(得分:1)
对于这样的事情,我建议使用事件总线。 Here is a link to a popular one I've had success with in the past.
基本上,你将有另一个课程,这将是你的公共汽车。您的活动将注册特定事件(您将创建,并根据需要进行子类化)。您的异步调用将告诉事件总线触发该事件,然后总线将告知所有订阅者,包括您的主要活动,事件被触发。那是你打电话给getDeviceLatestData
的时候。下面是您可能使用的简单代码片段,但请阅读该总线上的文档以完全理解它。
您的活动:
public static class DataReady Event { /* optional properties */ }
您的DeviceObject:
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
EventBus.getDefault().post(new DataReadyEvent());
}
} else {
Log.d("debg", "Error: " + e.getMessage());
}
}
});
}
}
您的主要活动:
public class MainActivity {
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
@Subscribe(threadMode = ThreadMode.MAIN) // Seems like you're updating UI, so use the main thread
public void onDataReady(DataReadyEvent event) {
/* Do whatever it is you need to do - remember you can add properties to your event and pull them off here if you need to*/
};
}