ListView未在自定义适配器

时间:2016-02-29 17:32:58

标签: android listview android-adapter custom-adapter

我正在尝试在新邮件到达时刷新ListView,但是一旦添加了邮件,我似乎无法刷新listview / adapter。 NotifyDataSetChangers()似乎不起作用。任何帮助将不胜感激。

MessagingActivity

public class MessagingActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    UserCredentialHandler userCredentialHandler;
    ListView chatListView;

    List<ChatRowItem> chatRowItems;
    List<Message> messagesList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messaging);
        userCredentialHandler = new UserCredentialHandler();
        Button bMsgJas = (Button)findViewById(R.id.bJas);
        Button bMsgTest = (Button)findViewById(R.id.bTest);
        Button bSendMsg = (Button)findViewById(R.id.button_send_message);
        final EditText inputText = (EditText)findViewById(R.id.chat_enter_message);

        final String userH = "h@h.com";
        final String userT = "test@test.com";

        chatRowItems = new ArrayList<>();
        final ChatCustomAdapter adapter = new ChatCustomAdapter(this, chatRowItems);
        chatListView = (ListView)findViewById(R.id.chat_listview);
        chatListView.setAdapter(adapter);
        chatListView.setOnItemClickListener(this);


        bMsgJas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SendBird.startMessaging(userH);
            }
        });

        bMsgTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SendBird.startMessaging(userT);
            }
        });

        bSendMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newMsg = inputText.getText().toString();
                SendBird.send(newMsg);
            }
        });

        SendBird.setEventHandler(new SendBirdEventHandler() {
            @Override
            public void onConnect(Channel channel) {

            }

            @Override
            public void onError(int i) {

            }

            @Override
            public void onChannelLeft(Channel channel) {

            }

            @Override
            public void onMessageReceived(Message message) {
                messagesList.add(message);

                adapter.updateChatList(chatRowItems);
            }

            @Override
            public void onSystemMessageReceived(SystemMessage systemMessage) {

            }

            @Override
            public void onBroadcastMessageReceived(BroadcastMessage broadcastMessage) {

            }

            @Override
            public void onFileReceived(FileLink fileLink) {

            }

            @Override
            public void onReadReceived(ReadStatus readStatus) {

            }

            @Override
            public void onTypeStartReceived(TypeStatus typeStatus) {

            }

            @Override
            public void onTypeEndReceived(TypeStatus typeStatus) {

            }

            @Override
            public void onAllDataReceived(SendBird.SendBirdDataType sendBirdDataType, int i) {

            }

            @Override
            public void onMessageDelivery(boolean b, String s, String s1, String s2) {

            }

            @Override
            public void onMessagingStarted(final MessagingChannel messagingChannel) {

                SendBird.queryMessageList(messagingChannel.getUrl()).load(Long.MAX_VALUE, 30, 10, new MessageListQuery.MessageListQueryResult() {
                    @Override
                    public void onResult(List<MessageModel> messageModels) {
                         messagesList = (List<Message>)(List<?>) messageModels;
                         for(int i = 0; i < messageModels.size(); i++){
                             drawListRows(messagesList.get(i));
                         }
                        SendBird.join(messagingChannel.getUrl());
                        SendBird.connect();
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
            }

            @Override
            public void onMessagingUpdated(MessagingChannel messagingChannel) {
            }

            @Override
            public void onMessagingEnded(MessagingChannel messagingChannel) {

            }

            @Override
            public void onAllMessagingEnded() {

            }

            @Override
            public void onMessagingHidden(MessagingChannel messagingChannel) {

            }

            @Override
            public void onAllMessagingHidden() {

            }
        });


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        //final String member_list_id = rowItems.get(position).bookListId;

    }

    public void drawListRows(Message msg){
        ChatRowItem item = new ChatRowItem(msg.getSenderName(), msg.getMessage(), msg.getTimestamp());
        chatRowItems.add(item);
    }

  //  public void updateChatList(List<ChatRowItem> chatItems){
  //      messagesList.clear();
  //      messagesList.addAll(newMsgList);
    }

ChatCustomAdapter

public class ChatCustomAdapter extends BaseAdapter {
    List<ChatRowItem> chatRowItemList;
    Context context;

    ChatCustomAdapter(Context context, List<ChatRowItem> chatRowItemList){
        this.chatRowItemList = chatRowItemList;
        this.context = context;
    }

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

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

    @Override
    public long getItemId(int position){
        return chatRowItemList.indexOf(getItem(position));
    }

    private class ViewHolder {
        TextView username;
        TextView message;
        TextView timestamp;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if(convertView == null ) {
            convertView = mInflater.inflate(R.layout.chat_msg_item, null);
            holder = new ViewHolder();
            holder.message = (TextView)convertView.findViewById(R.id.chat_message_text);
            holder.username = (TextView)convertView.findViewById(R.id.chat_user_id);
            holder.timestamp = (TextView)convertView.findViewById(R.id.chat_timestamp);

            ChatRowItem row_pos = chatRowItemList.get(position);

            holder.message.setText(row_pos.message);
            holder.username.setText(row_pos.userId);
//            holder.timestamp.setText((int)row_pos.timestamp);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        return convertView;
    }

    public void updateChatList(List<ChatRowItem> chatItems){
        chatRowItemList.clear();
        chatItems.addAll(chatItems);
        this.notifyDataSetChanged();
    }


}

2 个答案:

答案 0 :(得分:2)

 chatItems.addAll(chatItems);

应该是

 chatRowItemList.addAll(chatItems);

但这还不够。 chatRowItemList.clear();正在删除chatRowItems中的所有项目,因为您将其作为对Adapter的构造函数的引用传递。

答案 1 :(得分:0)

我认为再次设置适配器可以解决