适用于自定义适配器的Android Firebase onChildChanged

时间:2019-02-01 12:30:12

标签: java android firebase firebase-realtime-database

虽然从做一则短消息应用Firebase in a Weekend: Android,我想更新我的MessageAdapter与ChildEventListener。使用addremoveonChildAddedonChildRemoved并没有发现任何问题,但是当我尝试更新onChildChanged时,似乎没有任何问题好。首先,更改后的孩子被第二次添加,没有更新。他们添加在列表的末尾,不上它的索引。最后,我使用辅助ArrayList进行了修改,只是想知道已更改子级的索引,将其从mFriendlyMessagesList中删除,然后在旧索引处添加新内容。

FriendlyMessage.java

/**
 * Copyright Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.firebase.udacity.friendlychat;

public class FriendlyMessage {

    private String text;
    private String name;
    private String photoUrl;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
}

MessageAdapter.java

public class MessageAdapter extends ArrayAdapter<FriendlyMessage> {
    public MessageAdapter(Context context, int resource, List<FriendlyMessage> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message, parent, false);
        }

        ImageView photoImageView = (ImageView) convertView.findViewById(R.id.photoImageView);
        TextView messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
        TextView authorTextView = (TextView) convertView.findViewById(R.id.nameTextView);

        FriendlyMessage message = getItem(position);

        boolean isPhoto = message.getPhotoUrl() != null;
        if (isPhoto) {
            messageTextView.setVisibility(View.GONE);
            photoImageView.setVisibility(View.VISIBLE);
            Glide.with(photoImageView.getContext())
                    .load(message.getPhotoUrl())
                    .into(photoImageView);
        } else {
            messageTextView.setVisibility(View.VISIBLE);
            photoImageView.setVisibility(View.GONE);
            messageTextView.setText(message.getText());
        }
        authorTextView.setText(message.getName());

        return convertView;
    }
}

ChildEventListener

    mFriendlyMessagesList = new ArrayList<>();
    mMessageAdapter = new MessageAdapter(this, R.layout.item_message, 
    mFriendlyMessagesList);
    mMessageListView.setAdapter(mMessageAdapter);

    mChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class);
                mFriendlyMessagesList.add(friendlyMessage);
                mKeys.add(dataSnapshot.getKey());
            }

            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                String key = dataSnapshot.getKey();
                int index = mKeys.indexOf(key);

                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();

                mFriendlyMessagesList.remove(index);

                Toast.makeText(getApplicationContext(), " " + index, Toast.LENGTH_SHORT).show();
                FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class);

                mFriendlyMessagesList.add(index, friendlyMessage);
                mMessageAdapter.notifyDataSetChanged();
            }

你可以看到,我的mKeys是辅助ArrayList中,我添加每个子键给它(onChildAdded)。当有新的变化,我从获取子指数mKeys,删除mFriendlyMessagesList,并将其添加回适配器。

我已经为此忙了几天,却没有找到任何好的答案。它的工作完全符合预期,但是我认为这不是最好的方法。伙计们,您能否说出这是否是解决此问题的更好方法?对我来说,这只是一个快速解决方案,而不是正确的方法。

PS:我知道这是没有意义的更新上聊天应用程序的消息,这只是巧合是在这种情况下

0 个答案:

没有答案