ListView适配器不起作用(Quickblox聊天应用程序)

时间:2017-12-27 17:07:03

标签: listview android-studio arraylist adapter quickblox

我正在创建一个MULTIPLE_CHOISE用户列表,当我完成对活动的编码时,我发现它没有显示任何内容...... 这个聊天应用程序基于Quickblox云消息传递平台,我真的坚持这一部分。

非常感谢那些花时间阅读代码的人,帮助我< 3意味着很多。

This are activities that come after logging in

XML:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.aryojj.swagmessenger.ListUsersActivity">


    <ListView
        android:id="@+id/list_Users"
        android:layout_marginBottom="0dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>


    <Button
        android:layout_margin="10dp"
        android:background="@drawable/btnbg"
        android:id="@+id/btn_create_chat"
        android:text="New Chat"
        android:paddingEnd="10dp"
        android:textAllCaps="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:drawableStart="@mipmap/ic_add"
        android:layout_height="wrap_content" />

</RelativeLayout>

ListUsersActivity.java:

package com.aryojj.swagmessenger;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.amulyakhare.textdrawable.TextDrawable;
import com.aryojj.swagmessenger.Adapter.ListUsersAdapter;
import com.aryojj.swagmessenger.Common.Common;
import com.aryojj.swagmessenger.Holder.QBUsersHolder;
import com.quickblox.chat.QBChatService;
import com.quickblox.chat.QBRestChatService;
import com.quickblox.chat.model.QBChatDialog;
import com.quickblox.chat.model.QBDialogType;
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 java.util.ArrayList;

public class ListUsersActivity extends AppCompatActivity {

    ListView lstUsers;
    Button btnCreateChat;



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

        retrieveAllUser();

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

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

                int countChoise = lstUsers.getCount();

                if (lstUsers.getCheckedItemPositions().size() == 1)
                    createPrivateChat(lstUsers.getCheckedItemPositions());
                else if (lstUsers.getCheckedItemPositions().size()>1)
                    createGroupChat(lstUsers.getCheckedItemPositions());
                else
Toast.makeText(ListUsersActivity.this, 
"You should at least select 1 person to chat with!",
 Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void createGroupChat(SparseBooleanArray checkedItemPositions) {

        final ProgressDialog mDialog = 
new ProgressDialog(ListUsersActivity.this);

        mDialog.setMessage("Loading...");
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();

        int countChoise = lstUsers.getCount();
        ArrayList<Integer> occupantIdsList = new ArrayList<>();
        for (int i=0;i<countChoise;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(),
"Successfully created new chat!",Toast.LENGTH_SHORT).show();

                finish();
            }

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

    private void createPrivateChat
(SparseBooleanArray checkedItemPositions) {


        final ProgressDialog mDialog = 
new ProgressDialog(ListUsersActivity.this);

        mDialog.setMessage("Loading...");
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();

        int countChoise = lstUsers.getCount();
        for (int i=0;i<countChoise;i++)
        {
            if (checkedItemPositions.get(i))
            {
                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(),
"Successfully created new chat!",Toast.LENGTH_SHORT).show();

                        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:

package com.aryojj.swagmessenger.Adapter;

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.aryojj.swagmessenger.R;
import com.quickblox.users.model.QBUser;

import java.util.ArrayList;

/**
 * Created by Aryojj on 12/27/2017.
 */

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;
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,一切都很好,我所要做的就是去管理局/用户/设置并启用&#34;允许通过API&#34;来检索用户列表。