我进行了一个解析查询,现在我需要实现实时查询。但是我的查询有一个List<ParseObject>
,在这里的liveQuery
方法中我发现它是不同的。
public void onEvent(ParseQuery<ParseObject> query, final ParseObject object) {
ParseQuery是此方法的功能-请参阅我之前的post)来查看。
这是文档中建议的代码:
if (parseLiveQueryClient != null) {
ParseQuery<ParseObject> parseQuery = new ParseQuery("Message");
parseQuery.whereEqualTo("destination", "pokelist");
SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery);
subscriptionHandling.handleEvent(SubscriptionHandling.Event.CREATE, new SubscriptionHandling.HandleEventCallback<ParseObject>() {
@Override
public void onEvent(ParseQuery<ParseObject> query, final ParseObject object) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
EditText pokeText = findViewById(R.id.pokeText);
numPokes++;
if(numPokes == 1) {
pokeText.setText("Poked " + numPokes + " time.");
}
else {
pokeText.setText("Poked " + numPokes + " times.");
}
}
});
}
});}
我这样做了,并确实更新了我的消息列表。有什么建议么?我是解析新手,很困惑!
ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
queryChat = ParseObject.createWithoutData("Chat", idChatSeleccionadoAppUsuario);
query.whereEqualTo("ChatId", queryChat);
query.include("Usuario");
query.include("ChatId");
SubscriptionHandling<ParseObject> subscriptionHandling= App.parseLiveQueryClient.subscribe(query);
subscriptionHandling.handleEvent(SubscriptionHandling.Event.CREATE, new SubscriptionHandling.HandleEventCallback<ParseObject>() {
@Override
public void onEvent(final ParseQuery<ParseObject> query, final ParseObject object) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
listaDeMensajes.add(object);
}
});
mMessageAdapter = new MessageListAdapter(getContext(), listaDeMensajes);
mMessageRecycler.setAdapter(mMessageAdapter);
queryFromChat();
}
});
答案 0 :(得分:0)
我解决了我的问题,这就是...
首先像这样进行订阅。
private void changeChatLive(){
listaDeMensajes.clear();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
// Connect to Parse server
SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(query);
// Listen for CREATE events
subscriptionHandling.handleEvent(SubscriptionHandling.Event.CREATE, new
SubscriptionHandling.HandleEventCallback<ParseObject>() {
@Override
public void onEvent(ParseQuery<ParseObject> query, ParseObject object) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
mMessageAdapter.notifyDataSetChanged();
mMessageRecycler.scrollToPosition(0);
}
});
}
});
}
然后,我在这里调用该方法,然后运行liveChat。注意:在需要实时查询更新的地方调用方法。
queryConversaciones();
mMessageAdapter.notifyDataSetChanged();
**changeChatLive();**