QuickBlox检索聊天对话Android

时间:2017-10-04 02:38:18

标签: android quickblox

我正在用quickblox构建一个Android聊天应用程序.. 当我想检索聊天对话框列表和用户列表时,它没有出现在我的屏幕上.. 我已经搜索了这个问题,我发现我必须启用用户列表检索.. 所以我这样做但没有改变...... 这是我的ChatDialogsList代码:

package shashoug.com.zoolisd;

    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.support.design.widget.FloatingActionButton;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.ListView;
    import com.quickblox.auth.QBAuth;
    import com.quickblox.auth.session.BaseService;
    import com.quickblox.auth.session.QBSession;
    import com.quickblox.auth.session.QBSettings;
    import com.quickblox.chat.QBChatService;
    import com.quickblox.chat.QBRestChatService;
    import com.quickblox.chat.model.QBChatDialog;
    import com.quickblox.core.QBEntityCallback;
    import com.quickblox.core.exception.BaseServiceException;
    import com.quickblox.core.exception.QBResponseException;
    import com.quickblox.core.request.QBRequestGetBuilder;
    import com.quickblox.users.model.QBUser;
    import java.util.ArrayList;
    import shashoug.com.zoolisd.Adapter.ChatDialogAdapter;

    public class ChatDialogActivity extends AppCompatActivity {
    private ListView chatListDialogs;
    private FloatingActionButton floatingActionButton;

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

        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        chatListDialogs = (ListView) findViewById(R.id.lstChatsDialog);
        createSessionForChat();
        loadAllChat();
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChatDialogActivity.this,ListUsersActivity.class);
                startActivity(intent);
            }
        });
    }
    private void loadAllChat() {
        QBRequestGetBuilder requestBuilder = new QBRequestGetBuilder();
        requestBuilder.setLimit(100);
        QBRestChatService.getChatDialogs(null,requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBChatDialog>>() {
            @Override
            public void onSuccess(ArrayList<QBChatDialog> qbChatDialogs, Bundle bundle) {
                ChatDialogAdapter adapter = new ChatDialogAdapter(getBaseContext(),qbChatDialogs);
                chatListDialogs.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onError(QBResponseException e) {
              Log.e("ERROR",e.getMessage());
            }
        });
    }
    private void createSessionForChat() {
        final ProgressDialog mDialog = new ProgressDialog(ChatDialogActivity.this);
        mDialog.setTitle("Retreiveing The Dialogs");
        mDialog.setMessage("Please Wait While We Collecting your Chats Info.");
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();
        String name,password;
        name = getIntent().getStringExtra("name");
        password = getIntent().getStringExtra("password");
         final QBUser qbUser = new QBUser(name,password);
        QBAuth.createSession(qbUser).performAsync(new QBEntityCallback<QBSession>() {
            @Override
            public void onSuccess(QBSession qbSession, Bundle bundle) {
                mDialog.dismiss();
                qbUser.setId(qbSession.getUserId());
                try {
                    qbUser.setPassword(BaseService.getBaseService().getToken());
                } catch (BaseServiceException e) {
                    e.printStackTrace();
                }
                QBChatService.getInstance().login(qbUser, new QBEntityCallback() {
                    @Override
                    public void onSuccess(Object o, Bundle bundle) {
                        mDialog.dismiss();
                    }

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

            }

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

这是我的ChatsDialogAdapter代码:

package shashoug.com.zoolisd.Adapter;

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

    import com.amulyakhare.textdrawable.TextDrawable;
    import com.amulyakhare.textdrawable.util.ColorGenerator;
    import com.quickblox.chat.model.QBChatDialog;

    import java.util.ArrayList;

    import shashoug.com.zoolisd.R;

    public class ChatDialogAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<QBChatDialog> qbChatDialogs;

    public ChatDialogAdapter(Context context, ArrayList<QBChatDialog> qbChatDialogs) {
        this.context = context;
        this.qbChatDialogs = qbChatDialogs;
    }

        @Override
        public int getCount() {
        return qbChatDialogs.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view ==null)
        {
          LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_chat_dialog,null);
            TextView txtTitle;
            TextView txtMessage;
            ImageView imageView;
            txtTitle = (TextView) view.findViewById(R.id.list_chat_dialog_title);
            txtMessage = (TextView) view.findViewById(R.id.list_chat_dialog_message);
            imageView = (ImageView) view.findViewById(R.id.image_chat_dialog);
            txtMessage.setText(qbChatDialogs.get(position).getLastMessage());
            txtTitle.setText(qbChatDialogs.get(position).getName());
            ColorGenerator generator = ColorGenerator.MATERIAL;
            int randomColor = generator.getRandomColor();
            TextDrawable.IBuilder builder = TextDrawable.builder().beginConfig()
                    .withBorder(4)
                    .endConfig()
                    .round();
            TextDrawable drawable = builder.build(txtTitle.getText().toString().substring(0,1).toUpperCase(),randomColor);
            imageView.setImageDrawable(drawable);





        }
        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

在您的情况下,您调用异步方法createSessionForChat()并立即调用方法loadAllChat(),在创建会话的同时开始加载对话框。成功创建QB会话后,您需要调用方法loadAllChat()。