只有10个用户出现在用户列表中

时间:2017-08-24 23:30:42

标签: android quickblox

我正在使用quickblox开发一个消息传递应用程序,这是数据库中所有用户都显示的活动,当用户数量超过10时,只有10个用户显示。任何帮助都将非常感谢!!必须添加更多细节,但这应该足以解释问题

ListUsers类

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.liftersheaven.messaging.Adapter.ListUsersAdapter;
import com.liftersheaven.messaging.Common.Common;
import com.liftersheaven.messaging.Holder.QBUsersHolder;
import com.quickblox.chat.QBChatService;
import com.quickblox.chat.QBRestChatService;
import com.quickblox.chat.QBSystemMessagesManager;
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.utils.DialogUtils;
import com.quickblox.core.QBEntityCallback;
import com.quickblox.core.exception.QBResponseException;
import com.quickblox.users.QBUsers;
import com.quickblox.users.model.QBUser;

import org.jivesoftware.smack.SmackException;

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


public class ListUsers extends AppCompatActivity {

ListView lstUsers;
Button btnCreate;


String mode="";
QBChatDialog qbChatDialog;
List<QBUser> userAdd=new ArrayList<>();

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

    Toolbar toolbar = (Toolbar)findViewById(R.id.chatusers_toolbar);
    toolbar.setTitle("Users");
    setSupportActionBar(toolbar);

    mode = getIntent().getStringExtra(Common.UPDATE_MODE);
    qbChatDialog=(QBChatDialog)getIntent().getSerializableExtra(Common.UPDATE_DIALOG_EXTRA);

    lstUsers = (ListView)findViewById(R.id.lstUsers);
    lstUsers.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    btnCreate = (Button)findViewById(R.id.btn_create_chat);
    btnCreate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (mode == null) {


                int countChoice = lstUsers.getCount();

                if (lstUsers.getCheckedItemPositions().size() == 1)
                    createPrivateChat(lstUsers.getCheckedItemPositions());
                else if (lstUsers.getCheckedItemPositions().size() > 1)
                    createGroupChat(lstUsers.getCheckedItemPositions());
                else
                    Toast.makeText(ListUsers.this, "Select a friend to chat with", Toast.LENGTH_SHORT).show();

            } else if (mode.equals(Common.UPDATE_ADD_MODE) && qbChatDialog != null) {
                if (userAdd.size() > 0) {
                    QBDialogRequestBuilder requestBuilder = new QBDialogRequestBuilder();

                    int cntChoice = lstUsers.getCount();
                    SparseBooleanArray checkItemPositions = lstUsers.getCheckedItemPositions();
                    for (int i = 0; i < cntChoice; i++) {
                        if (checkItemPositions.get(i)) {
                            QBUser user = (QBUser) lstUsers.getItemAtPosition(i);
                            requestBuilder.addUsers(user);
                        }
                    }
                    QBRestChatService.updateGroupChatDialog(qbChatDialog, requestBuilder)
                            .performAsync(new QBEntityCallback<QBChatDialog>() {
                                @Override
                                public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {
                                    Toast.makeText(getBaseContext(), "Add user success", Toast.LENGTH_SHORT).show();
                                    finish();
                                }

                                @Override
                                public void onError(QBResponseException e) {

                                }
                            });
                }
            } else if (mode.equals(Common.UPDATE_REMOVE_MODE) && qbChatDialog != null) {
                if (userAdd.size() > 0) {
                    QBDialogRequestBuilder requestBuilder = new QBDialogRequestBuilder();
                    int cntChoice = lstUsers.getCount();
                    SparseBooleanArray checkItemPositions = lstUsers.getCheckedItemPositions();
                    for (int i = 0; i < cntChoice; i++) {
                        if (checkItemPositions.get(i)) {
                            QBUser user = (QBUser) lstUsers.getItemAtPosition(i);
                            requestBuilder.removeUsers(user);
                        }
                    }
                    QBRestChatService.updateGroupChatDialog(qbChatDialog, requestBuilder)
                            .performAsync(new QBEntityCallback<QBChatDialog>() {
                                @Override
                                public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {
                                    Toast.makeText(getBaseContext(), "Remove user success", Toast.LENGTH_SHORT).show();
                                    finish();
                                }

                                @Override
                                public void onError(QBResponseException e) {

                                }
                            });
                }
            }
        }
    });

    if (mode == null && qbChatDialog == null)
        retrieveAllUser();
    else{
        if (mode.equals(Common.UPDATE_ADD_MODE))
            loadListAvailableUser();
        else if (mode.equals(Common.UPDATE_REMOVE_MODE))
            loadListUserInGroup();
    }
}

private void loadListUserInGroup() {

    btnCreate.setText("Remove User");
    QBRestChatService.getChatDialogById(qbChatDialog.getDialogId())
            .performAsync(new QBEntityCallback<QBChatDialog>() {
                @Override
                public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {
                    List<Integer> occupantsId = qbChatDialog.getOccupants();
                    List<QBUser> listUserAlreadyInGroup = QBUsersHolder.getInstance().getUsersByIds(occupantsId);
                    ArrayList<QBUser> users = new ArrayList<QBUser>();
                    users.addAll(listUserAlreadyInGroup);

                    ListUsersAdapter adapter = new ListUsersAdapter(getBaseContext(),users);
                    lstUsers.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    userAdd = users;
                }

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

private void loadListAvailableUser() {
    btnCreate.setText("Add User");

    QBRestChatService.getChatDialogById(qbChatDialog.getDialogId())
            .performAsync(new QBEntityCallback<QBChatDialog>() {
                @Override
                public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {
                    ArrayList<QBUser> listUsers = QBUsersHolder.getInstance().getAllUsers();
                    List<Integer> occupantsId = qbChatDialog.getOccupants();
                    List<QBUser>listUserAlreadyInChatGroup = QBUsersHolder.getInstance().getUsersByIds(occupantsId);

                    for (QBUser user:listUserAlreadyInChatGroup)
                        listUsers.remove(user);
                    if (listUsers.size() > 0){
                        ListUsersAdapter adapter = new ListUsersAdapter(getBaseContext(),listUsers);
                        lstUsers.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        userAdd = listUsers;
                    }
                }

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

                }
            });
}

private void createGroupChat(SparseBooleanArray checkedItemPositions) {

    final ProgressDialog mDialog = new ProgressDialog(ListUsers.this);
    mDialog.setMessage("Waiting...");
    mDialog.setCanceledOnTouchOutside(false);
    mDialog.show();

    int countChoice = lstUsers.getCount();
    ArrayList<Integer> occupantIdsList = new ArrayList<>();
    for (int i =0;i<countChoice;i++){
        if (checkedItemPositions.get(i)){
            QBUser user = (QBUser)lstUsers.getItemAtPosition(i);
            occupantIdsList.add(user.getId());
        }
    }

    QBChatDialog dialog = new QBChatDialog();
    dialog.setName(Common.createChatDialogName(occupantIdsList));
    dialog.setType(QBDialogType.GROUP);
    dialog.setOccupantsIds(occupantIdsList);

    QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() {
        @Override
        public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {
            mDialog.dismiss();
            Toast.makeText(getBaseContext(), "Chat dialog successfully created", Toast.LENGTH_SHORT).show();
            finish();

            QBSystemMessagesManager qbSystemMessagesManager = QBChatService.getInstance().getSystemMessagesManager();
            QBChatMessage qbChatMessage = new QBChatMessage();
            qbChatMessage.setBody(qbChatDialog.getDialogId());
            for (int i=0;i<qbChatDialog.getOccupants().size();i++) {
                qbChatMessage.setRecipientId(qbChatDialog.getOccupants().get(i));
                try {
                    qbSystemMessagesManager.sendSystemMessage(qbChatMessage);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
            }


            finish();
        }

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

private void createPrivateChat(SparseBooleanArray checkedItemPositions) {

    final ProgressDialog mDialog = new ProgressDialog(ListUsers.this);
    mDialog.setMessage("Waiting...");
    mDialog.setCanceledOnTouchOutside(false);
    mDialog.show();

    int countChoice = lstUsers.getCount();
    for(int i=0;i<countChoice;i++){
        if (checkedItemPositions.get(i)){
            final QBUser user = (QBUser)lstUsers.getItemAtPosition(i);
            QBChatDialog dialog = DialogUtils.buildPrivateDialog(user.getId());

            QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() {
                @Override
                public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {
                    mDialog.dismiss();
                    Toast.makeText(getBaseContext(), "Private chat dialog successfully created", Toast.LENGTH_SHORT).show();
                    finish();

                    QBSystemMessagesManager qbSystemMessagesManager = QBChatService.getInstance().getSystemMessagesManager();
                    QBChatMessage qbChatMessage = new QBChatMessage();
                    qbChatMessage.setRecipientId(user.getId());
                    qbChatMessage.setBody(qbChatDialog.getDialogId());
                    try {
                        qbSystemMessagesManager.sendSystemMessage(qbChatMessage);
                    } catch (SmackException.NotConnectedException e) {
                        e.printStackTrace();
                    }

                    finish();

                }

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

                }
            });
        }
    }

}

private void retrieveAllUser() {

    QBUsers.getUsers(null).performAsync(new QBEntityCallback<ArrayList<QBUser>>() {
        @Override
        public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {

            QBUsersHolder.getInstance().putUsers(qbUsers);

            ArrayList<QBUser> qbUserWithoutCurrent = new ArrayList<QBUser>();
            for (QBUser user : qbUsers){
                if (!user.getLogin().equals(QBChatService.getInstance().getUser().getLogin()))
                    qbUserWithoutCurrent.add(user);
            }

            ListUsersAdapter adapter = new ListUsersAdapter(getBaseContext(),qbUserWithoutCurrent);
            lstUsers.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

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


}

ListUsersAdapter类

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.quickblox.users.model.QBUser;

import java.util.ArrayList;



public class ListUsersAdapter extends BaseAdapter {

private Context context;
private ArrayList<QBUser> qbUserArrayList;

public ListUsersAdapter(Context context, ArrayList<QBUser> qbUserArrayList){
this.context = context;
    this.qbUserArrayList = qbUserArrayList;
}

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

@Override
public Object getItem(int position) {
    return qbUserArrayList.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);

        view = inflater.inflate(android.R.layout.simple_list_item_multiple_choice,null);
        TextView textView = (TextView)view.findViewById(android.R.id.text1);
        textView.setText(qbUserArrayList.get(position).getLogin());
    }
    return view;
}
}

QBUserHolder Class

import android.util.SparseArray;

import com.quickblox.users.model.QBUser;

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




public class QBUsersHolder {

private static  QBUsersHolder instance;

private SparseArray<QBUser> qbUserSparseArray;

public static synchronized QBUsersHolder getInstance(){
    if (instance == null)
        instance = new QBUsersHolder();
    return instance;
}

private QBUsersHolder(){
    qbUserSparseArray = new SparseArray<>();
}

public void  putUsers(List<QBUser> users){
    for (QBUser user:users)
        putUser(user);
}

public void putUser(QBUser user) {
    qbUserSparseArray.put(user.getId(),user);
}

public QBUser getUserById(int id){
    return qbUserSparseArray.get(id);}

public List<QBUser> getUsersByIds(List<Integer> ids){
List<QBUser> qbUser = new ArrayList<>();
for(Integer id:ids) {
    QBUser user = getUserById(id);
    if (user != null)
        qbUser.add(user);
}
    return qbUser;
}

public ArrayList<QBUser> getAllUsers() {
    ArrayList<QBUser> result = new ArrayList<>();
    for (int i=0;i<qbUserSparseArray.size();i++)
        result.add(qbUserSparseArray.valueAt(i));
    return result;
}
}

1 个答案:

答案 0 :(得分:0)

默认情况下,每页只能检索10个用户。您应该更改检索用户的JSON请求

相关问题