2017-12-27 445 views
0

我正在創建一個MULTIPLE_CHOISE的用戶列表,當我完成編碼適配器的活動,我發現它沒有顯示任何東西... 這個聊天應用程序是基於Quickblox雲消息傳遞平臺,我真的被困在這一部分。ListView適配器不工作(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; 
    } 
} 
+0

爲什麼android.R.id? –

+0

@ Md.ibrahimkhalil我想它會創建一個視圖,如果它不存在,對吧? –

+0

可能不是這種情況。嘗試添加布局的textview的ID –

回答

0

好了,一切OK了,所有我有要做的是去adminPanel /用戶/設置,並啓用「允許通過API檢索用戶列表」。