第一条消息重复

时间:2017-08-24 06:14:53

标签: android android-studio quickblox

我一直在处理一个消息传递应用程序,在群组和私人聊天中,第一条消息重复,然后当另一条消息发送时,它隐藏在重复文本后面,直到发送另一条消息然后等等。任何人都可以帮我解决这个问题吗?我正在使用Quickblox作为数据库

ChatMessage类

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.liftersheaven.messaging.Adapter.ChatMessageAdapter;
import com.liftersheaven.messaging.Common.Common;
import com.liftersheaven.messaging.Holder.QBChatMessagesHolder;
import com.quickblox.chat.QBChatService;
import com.quickblox.chat.QBIncomingMessagesManager;
import com.quickblox.chat.QBRestChatService;
import com.quickblox.chat.exception.QBChatException;
import com.quickblox.chat.listeners.QBChatDialogMessageListener;
import com.quickblox.chat.model.QBChatDialog;
import com.quickblox.chat.model.QBChatMessage;
import com.quickblox.chat.model.QBDialogType;
import com.quickblox.chat.request.QBDialogRequestBuilder;
import com.quickblox.chat.request.QBMessageGetBuilder;
import com.quickblox.chat.request.QBMessageUpdateBuilder;
import com.quickblox.core.QBEntityCallback;
import com.quickblox.core.exception.QBResponseException;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smackx.muc.DiscussionHistory;

import java.util.ArrayList;


public class ChatMessage extends AppCompatActivity  implements 
QBChatDialogMessageListener{

QBChatDialog qbChatDialog;
ListView lstChatMessages;
ImageButton submitButton;
EditText edtContent;

ChatMessageAdapter adapter;


int contextMenuIndexClicked = -1;
boolean isEditMode = false;
QBChatMessage editMessage;



Toolbar toolbar;


@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId()){
        case R.id.chat_group_edit_name:
            editNameGroup();
            break;
        case R.id.chat_group_add_user:
            addUser();
            break;
        case R.id.chat_group_remove_user:
            removeUser();
            break;
    }
    return true;
}

private void removeUser() {
    Intent intent = new Intent(this,ListUsers.class);
    intent.putExtra(Common.UPDATE_DIALOG_EXTRA, qbChatDialog);
    intent.putExtra(Common.UPDATE_MODE, Common.UPDATE_REMOVE_MODE);
    startActivity(intent);
}

private void addUser() {
    Intent intent = new Intent(this,ListUsers.class);
    intent.putExtra(Common.UPDATE_DIALOG_EXTRA, qbChatDialog);
    intent.putExtra(Common.UPDATE_MODE,Common.UPDATE_ADD_MODE);
    startActivity(intent);
}

private void editNameGroup() {
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.dialog_edit_group_layout, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(view);
    final EditText newName = (EditText) view.findViewById(R.id.edt_group_name);

    alertDialogBuilder.setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    qbChatDialog.setName(newName.getText().toString());

                    QBDialogRequestBuilder requestBuilder = new QBDialogRequestBuilder();
                    QBRestChatService.updateGroupChatDialog(qbChatDialog, requestBuilder)
                            .performAsync(new QBEntityCallback<QBChatDialog>() {
                                @Override
                                public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {
                                    Toast.makeText(ChatMessage.this, "Group name edited", Toast.LENGTH_SHORT);
                                    toolbar.setTitle(qbChatDialog.getName());
                                }

                                @Override
                                public void onError(QBResponseException e) {
                                    Toast.makeText(getBaseContext(), ""+e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    if (qbChatDialog.getType() == QBDialogType.GROUP || qbChatDialog.getType() == QBDialogType.PUBLIC_GROUP)
        getMenuInflater().inflate(R.menu.chat_message_group_menu, menu);

    return true;
}

@Override
public boolean onContextItemSelected(MenuItem item){

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    contextMenuIndexClicked = info.position;

    switch (item.getItemId()){
        case R.id.chat_message_update:
            updateMessage();
            break;
        case R.id.chat_message_delete:
            deleteMessage();
            break;
    }
    return true;
}

private void deleteMessage() {
    final ProgressDialog deleteDialog = new ProgressDialog(ChatMessage.this);
    deleteDialog.setMessage("Please wait...");
    deleteDialog.show();

    editMessage = QBChatMessagesHolder.getInstance().getChatMessagesByDialogId(qbChatDialog.getDialogId())
            .get(contextMenuIndexClicked);

    QBRestChatService.deleteMessage(editMessage.getId(),false).performAsync(new QBEntityCallback<Void>() {
        @Override
        public void onSuccess(Void aVoid, Bundle bundle) {
            retrieveAllMessage();
            deleteDialog.dismiss();
        }

        @Override
        public void onError(QBResponseException e) {

        }
    });
}

private void updateMessage() {
    editMessage = QBChatMessagesHolder.getInstance().getChatMessagesByDialogId(qbChatDialog.getDialogId())
            .get(contextMenuIndexClicked);
    edtContent.setText(editMessage.getBody());
    isEditMode = true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
    getMenuInflater().inflate(R.menu.chat_message_content_menu, menu);
}


@Override
protected void onDestroy(){
    super.onDestroy();
    qbChatDialog.removeMessageListrener(this);
}

@Override
protected void onStop(){
    super.onStop();
    qbChatDialog.removeMessageListrener(this);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat_message);



    initViews();

    initChatDialogs();

    retrieveAllMessage();


    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!edtContent.getText().toString().isEmpty()){
            if (!isEditMode) {
                QBChatMessage chatMessage = new QBChatMessage();
                chatMessage.setBody(edtContent.getText().toString());
                chatMessage.setSenderId(QBChatService.getInstance().getUser().getId());
                chatMessage.setSaveToHistory(true);

                try {
                    qbChatDialog.sendMessage(chatMessage);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }


                if (qbChatDialog.getType() == QBDialogType.PRIVATE){

                    QBChatMessagesHolder.getInstance().putMessage(qbChatDialog.getDialogId(),chatMessage);
                    ArrayList<QBChatMessage> messages = QBChatMessagesHolder.getInstance().getChatMessagesByDialogId(chatMessage.getDialogId());
                    adapter = new ChatMessageAdapter(getBaseContext(),messages);
                    lstChatMessages.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }


                edtContent.setText("");
                edtContent.setFocusable(true);

            }else
            {
                final ProgressDialog updateDialog = new ProgressDialog(ChatMessage.this);
                updateDialog.setMessage("Please wait...");
                updateDialog.show();

                QBMessageUpdateBuilder messageUpdateBuilder = new QBMessageUpdateBuilder();
                messageUpdateBuilder.updateText(edtContent.getText().toString()).markDelivered().markRead();

                QBRestChatService.updateMessage(editMessage.getId(),qbChatDialog.getDialogId(),messageUpdateBuilder)
                        .performAsync(new QBEntityCallback<Void>() {
                            @Override
                            public void onSuccess(Void aVoid, Bundle bundle) {
                                retrieveAllMessage();
                                isEditMode = false;
                                updateDialog.dismiss();

                                edtContent.setText("");
                                edtContent.setFocusable(true);
                            }

                            @Override
                            public void onError(QBResponseException e) {
                                Toast.makeText(getBaseContext(),""+e.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        });
            }
            }
        }
    });

}

private void retrieveAllMessage() {
    QBMessageGetBuilder messageGetBuilder = new QBMessageGetBuilder();
    messageGetBuilder.setLimit(500);

    if(qbChatDialog != null){
        QBRestChatService.getDialogMessages(qbChatDialog,messageGetBuilder).performAsync(new QBEntityCallback<ArrayList<QBChatMessage>>() {
            @Override
            public void onSuccess(ArrayList<QBChatMessage> qbChatMessages, Bundle bundle) {

                QBChatMessagesHolder.getInstance().putMessages(qbChatDialog.getDialogId(),qbChatMessages);

                adapter = new ChatMessageAdapter(getBaseContext(),qbChatMessages);
                lstChatMessages.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(QBResponseException e) {

            }
        });
    }
}

private void initChatDialogs() {
    qbChatDialog = (QBChatDialog)getIntent().getSerializableExtra(Common.DIALOG_EXTRA);
    qbChatDialog.initForChat(QBChatService.getInstance());

    QBIncomingMessagesManager incomingMessage = QBChatService.getInstance().getIncomingMessagesManager();
    incomingMessage.addDialogMessageListener(new QBChatDialogMessageListener() {
        @Override
        public void processMessage(String s, QBChatMessage qbChatMessage, Integer integer) {

        }

        @Override
        public void processError(String s, QBChatException e, QBChatMessage qbChatMessage, Integer integer) {

        }
    });

    if (qbChatDialog.getType() == QBDialogType.PUBLIC_GROUP || qbChatDialog.getType() == QBDialogType.GROUP){
        DiscussionHistory discussionHistory = new DiscussionHistory();
        discussionHistory.setMaxStanzas(0);

        qbChatDialog.join(discussionHistory, new QBEntityCallback() {
            @Override
            public void onSuccess(Object o, Bundle bundle) {

            }

            @Override
            public void onError(QBResponseException e) {
                Log.d("ERROR", ""+e.getMessage());

            }
        });


    }
    qbChatDialog.addMessageListener(this);

    toolbar.setTitle(qbChatDialog.getName());
    setSupportActionBar(toolbar);

}

private void initViews() {

    lstChatMessages = (ListView)findViewById(R.id.messages_list);
    submitButton = (ImageButton)findViewById(R.id.send);
    edtContent = (EditText)findViewById(R.id.edt_content);





    registerForContextMenu(lstChatMessages);

    toolbar = (Toolbar)findViewById(R.id.chatmessage_toolbar);

}



@Override
public void processMessage(String s, QBChatMessage qbChatMessage, Integer integer) {
    QBChatMessagesHolder.getInstance().putMessage(qbChatMessage.getDialogId(),qbChatMessage);
    ArrayList<QBChatMessage> messages = QBChatMessagesHolder.getInstance().getChatMessagesByDialogId(qbChatMessage.getDialogId());
    adapter = new ChatMessageAdapter(getBaseContext(),messages);
    lstChatMessages.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

@Override
public void processError(String s, QBChatException e, QBChatMessage qbChatMessage, Integer integer) {
    Log.e("ERROR",""+e.getMessage());
}
}

ChatMessageAdapter类

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.github.library.bubbleview.BubbleTextView;
import com.liftersheaven.messaging.Holder.QBUsersHolder;
import com.liftersheaven.messaging.R;
import com.quickblox.chat.QBChatService;
import com.quickblox.chat.model.QBChatMessage;

import java.util.ArrayList;



public class ChatMessageAdapter extends BaseAdapter {
private Context context;
private ArrayList<QBChatMessage> qbChatMessages;

public ChatMessageAdapter(Context context, ArrayList<QBChatMessage> qbChatMessages){
    this.context = context;
    this.qbChatMessages = qbChatMessages;
}
@Override
public int getCount() {
    return qbChatMessages.size();
}

@Override
public Object getItem(int position) {
    return qbChatMessages.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (qbChatMessages.get(position).getSenderId().equals(QBChatService.getInstance().getUser().getId())){
            view = inflater.inflate(R.layout.list_message_send, null);
            BubbleTextView bubbleTextView = (BubbleTextView)view.findViewById(R.id.message_content);
            bubbleTextView.setText(qbChatMessages.get(position).getBody());
        }
        else{
            view = inflater.inflate(R.layout.list_recieve_message, null);
            BubbleTextView bubbleTextView = (BubbleTextView)view.findViewById(R.id.message_recieve);
            bubbleTextView.setText(qbChatMessages.get(position).getBody());
            TextView txtName = (TextView)view.findViewById(R.id.message_user);
            txtName.setText(QBUsersHolder.getInstance().getUserById(qbChatMessages.get(position).getSenderId()).getFullName());
        }
    }
    return view;
}
}

QBChatMessagesHolder Class

import com.quickblox.chat.model.QBChatMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



public class QBChatMessagesHolder {
private static   QBChatMessagesHolder instance;
private HashMap<String,ArrayList<QBChatMessage>> qbChatMessageArray;

public static synchronized QBChatMessagesHolder getInstance(){
    QBChatMessagesHolder qbChatMessagesHolder;
    synchronized (QBChatMessagesHolder.class){
        if (instance == null)
            instance = new QBChatMessagesHolder();
        qbChatMessagesHolder = instance;
    }
    return qbChatMessagesHolder;
}

private QBChatMessagesHolder(){
    this.qbChatMessageArray = new HashMap<>();
}

public void putMessages(String dialogId,ArrayList<QBChatMessage> qbChatMessages){
    this.qbChatMessageArray.put(dialogId,qbChatMessages);
}

public void putMessage(String dialogId,QBChatMessage qbChatMessage){
    List<QBChatMessage> lstResult = (List)this.qbChatMessageArray.get(dialogId);
    lstResult.add(qbChatMessage);
    ArrayList<QBChatMessage> lstAdded = new ArrayList(lstResult.size());
    lstAdded.addAll(lstResult);
    putMessages(dialogId, lstAdded);
}

public ArrayList<QBChatMessage> getChatMessagesByDialogId(String dialogId){
    return (ArrayList<QBChatMessage>)this.qbChatMessageArray.get(dialogId);
}
}

0 个答案:

没有答案