ListView Adapter doesnt work (Quickblox chat app)

listview android-studio arraylist adapter quickblox

29 观看

1回复

8 作者的声誉

I was creating a list of user that are MULTIPLE_CHOISE and when I finished coding the adapter an the activity, I found out that it is not showing anything... this chat app is based on Quickblox cloud messaging platform and I'm really stuck on this part.

Thanks so much to whoever that is putting time into reading the code an helping me <3 means a lot.

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;
    }
}
作者: Aryo Jj 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

8 作者的声誉

Well, everything was OK and all i had to do was going to adminPanel/users/settings and enable "Allow to retrieve a list of users via API".

作者: Aryo Jj 发布者: 2017 年 12 月 27 日
32x32