我正在开发一个Android项目,我正在尝试集成Cometd框架提供的PUSH
服务。
现在,每当有Conversation
的新邮件到达时,我想通知ChatMessagesActivity
,其中包含两个用户之间的邮件列表。
现在,当其他用户向Android应用发送消息时,我想更新用户的视图。我尝试通过调用notifyDataSetHasChanged()
上的adapter
来做到这一点,但因为我在View
线程之外调用它,我收到错误。
该方法是静态的,因为在Conversation
类中接收新消息,而消息在ChatMessagesActivity
类中进行。对于两个类之间的通信,我创建了2个静态方法,它们就像发送和发送的双向桥。接收消息。
我希望我很清楚,如果有任何疑问,请告诉我。
ChatMessagesActivity.java:
public class ChatMessagesActivity extends ApplicationDrawerLoader {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_messages);
chatList = (ListView)findViewById(R.id.chatList);
new getPrivateChatsForUser(this).execute();
}
// IN the method below, I receive information from another activity.
public static void recieveUpdatedMessage(String channelName, Map<String, Object> input){
Boolean found = Arrays.asList(channelName.split(" ")).contains("chat");
if(found){
int processedChannelName = Integer.valueOf(channelName.replace("/chat/",""));
if(processedChannelName == groupAccountId){
// Here i tried calling adapter.NotifyDataSetchanged();.. Didn't fly.
}
}
}
// Async method retrieving messages.
public class getPrivateChatsForUser extends AsyncTask<Void, Void, ResponseEntity<RestReplies[]>> {
ChatMessagesActivity chatMessagesActivity = null;
getPrivateChatsForUser(ChatMessagesActivity chatMessagesActivity) {
this.chatMessagesActivity = chatMessagesActivity;
}
@Override
protected ResponseEntity<RestReplies[]> doInBackground(Void... params) {
// network connection related stuff, not relevant to problem
}
@Override
protected void onPostExecute(ResponseEntity<RestReplies[]> responseEntity) {
super.onPostExecute(responseEntity);
RestReplies[] restRepliesArray = responseEntity.getBody();
Collections.addAll(restRepliesList, restRepliesArray);
ArrayList<HashMap<String, String>> chatMessagesHashMapList = new ArrayList<HashMap<String, String>>();
for (RestReplies restReplies : restRepliesList) {
HashMap<String, String> chatMap = new HashMap<>();
chatMap.put(chatText, restReplies.getReplyText());
chatMap.put(firstName, restReplies.getReplyingPersonName());
chatMap.put(chatImage, restReplies.getSenderImage());
chatMap.put(privateChannel,"/service/person/"+String.valueOf(conversationId));
chatMessagesHashMapList.add(chatMap);
}
chatList = (ListView) findViewById(R.id.chatList);
chatMessagesAdapter = new ChatMessagesAdapter(chatMessagesActivity, chatMessagesHashMapList);
chatList.setAdapter(chatMessagesAdapter);
chatList.scrollTo(0, chatList.getHeight());
}
}
那么,我应该如何指示数据集已被更改。以及适配器如何知道在何处以及如何获取已更改的数据集。任何人都可以帮我解决这个问题。非常感谢......: - )
答案 0 :(得分:1)
在适配器上使用广播接收器,并在推送服务中使用Android LocalBroadcast发送本地广播
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("MYREFRESH"))
{
notifiyDataSetChanged();
}
}
};
在适配器中的构造函数中注册此接收器
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("MYREFRESH");
LocalBroadcastManager.getInstance(context).registerReceiver(broadReceiver, intentFilter);
如果您获得了推送通知,那么在您的推送中
Intent intent = new Intent();
intent.putExtra(...) //send any data to your adapter
Intent.setAction("myaction");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
答案 1 :(得分:0)
处理此问题的方法是使用广播或总线模式。您可以使用一些好的总线库,例如http://square.github.io/otto/或LocalBroadcast
我过去的回答显示了如何使用LocalBroadcast系统:Refreshing fragments in FragmentActivity after sync service runs