【问题标题】:How do I output messages in ListView from PubNub method history()?如何从 PubNub 方法 history() 在 ListView 中输出消息?
【发布时间】:2017-09-21 12:46:32
【问题描述】:

我的项目(简单聊天应用程序)中有 TextViews(消息)的 ListView。下面是发布消息的代码:

    public void publish(View view) {

    final EditText mMessage = (EditText) MainActivity.this.findViewById(R.id.new_message);

    final Map<String, String> message = ImmutableMap.<String, String>of("sender", MainActivity.this.nickName, "message", mMessage.getText().toString(), "timestamp", DateTimeUtil.getTimeStampUtc());
    MainActivity.this.mPubnub_DataStream.publish().channel(Constants.CHANNEL_NAME).shouldStore(true).message(message).async(
            new PNCallback<PNPublishResult>() {
                @Override
                public void onResponse(PNPublishResult result, PNStatus status) {
                    try {
                        if (!status.isError()) {
                            mMessage.setText("");
                            Log.v(TAG, "publish(" + JsonUtil.asJson(result) + ")");
                        } else {
                            Log.v(TAG, "publishErr(" + JsonUtil.asJson(status) + ")");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
    );
}

我正在尝试从历史记录中输出消息:

this.mPubnub_DataStream.history()
                .channel(Constants.CHANNEL_NAME)
                .async(new PNCallback<PNHistoryResult>() {
                    @Override
                    public void onResponse(PNHistoryResult result, PNStatus status) {

                    }
                });

我不知道如何覆盖 onResponse 方法。请帮我。 顺便说一句,我可以检查我的消息是否已存储,但输出到终端并不是我真正需要的。

pubNub.history()
                .channel(channelName)
                .count(100)
                .async(new PNCallback<PNHistoryResult>() {
                    @Override
                    public void onResponse(PNHistoryResult result, PNStatus status) {
                        for (int i = 0; i < 100; i++) {
                            System.out.println(result.getMessages().get(i).getEntry());
                        }
                    }
                });

【问题讨论】:

  • 这更像是一个 UI 数据绑定问题而不是 PubNub 问题,只要您实际接收来自 history 调用的消息。如果您是,那么此示例应用程序中的示例代码可能会有所帮助:github.com/pubnub/webinar-android-intro - 请告诉我。

标签: java listview pubnub


【解决方案1】:

这更像是一个 UI 数据绑定问题,而不是 PubNub 问题,只要您实际接收来自历史调用的消息。

如果您是,那么此示例应用程序中的示例代码可能会有所帮助:github.com/pubnub/webinar-android-intro

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多