为什么我无法从Firebase实时数据库中读取和显示数据?

时间:2017-07-07 10:44:46

标签: android firebase firebase-realtime-database

我正在尝试开发一个从Firebase数据库读取和显示数据的机器人,遗憾的是,我无法显示数据库中的任何数据。在Android监视器中没有出现任何错误消息或任何错误问题(我我正在使用Android Studio)。我注意到唯一的问题是在FriendlyMessage文件中,setText,setName,setPhotoUrl从未使用过这三种方法。

该项目有三个部分,1)主要活动,2)MessageAdapter,3)Friendlymessage 1)MaiinActivity

35.xxx.xx.xxx:30488

}  2)MessageAdapter:

 public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

public static final String ANONYMOUS = "anonymous";
public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;

private ListView mMessageListView;
private MessageAdapter mMessageAdapter;
private ProgressBar mProgressBar;
private ImageButton mPhotoPickerButton;
private EditText mMessageEditText;
private Button mSendButton;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mFirebaseDatabaseReference;
private ChildEventListener monchildlistner;
private String mUsername;

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

    mUsername = ANONYMOUS;

    mFirebaseDatabase=FirebaseDatabase.getInstance();
    mFirebaseDatabaseReference=mFirebaseDatabase.getReference().child("message");


    // Initialize references to views
    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    mMessageListView = (ListView) findViewById(R.id.messageListView);
    mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
    mMessageEditText = (EditText) findViewById(R.id.messageEditText);
    mSendButton = (Button) findViewById(R.id.sendButton);


    // Initialize message ListView and its adapter
    List<FriendlyMessage> friendlyMessage = new ArrayList<>();
    mMessageAdapter = new MessageAdapter(this, R.layout.item_message,friendlyMessage);
    mMessageListView.setAdapter(mMessageAdapter);

    // Initialize progress bar
    mProgressBar.setVisibility(ProgressBar.INVISIBLE);

    // ImagePickerButton shows an image picker to upload a image for a message
    mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO: Fire an intent to show an image picker
        }
    });

    // Enable Send button when there's text to send
    mMessageEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.toString().trim().length() > 0) {
                mSendButton.setEnabled(true);
            } else {
                mSendButton.setEnabled(false);
            }
        }
        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});

    // Send button sends a message and clears the EditText
    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO: Send messages on click
      FriendlyMessage friendlyMessage=new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
             mFirebaseDatabaseReference.push().setValue(friendlyMessage);
            // Clear input box
            mMessageEditText.setText("");
        }
    });
    monchildlistner=new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            FriendlyMessage friendlymessage=dataSnapshot.getValue(FriendlyMessage.class);
            mMessageAdapter.add(friendlymessage);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
       //when a message added on the database at that time addchildeventlistener will fire up and refer us to onchildadded method
    mFirebaseDatabaseReference.addChildEventListener(monchildlistner);

}

} 3)FriendlyMessage:

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

}

2 个答案:

答案 0 :(得分:1)

添加邮件后,您没有通知适配器刷新或加载新添加的内容。

adapter.notifyDataSetChanged()

会起作用

答案 1 :(得分:0)

我发现问题并且问题已经解决了,我将此主题标记为已解决的问题。问题出在我的布局文件中。不幸的是,我被分配了列表视图值&#39; 0&#39 ;,改变了值&#39; 0&#39;为了让父母能够正常工作。感谢所有人的支持。