android列表视图不能从数据库动态填充

bpzcxfmw  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(197)

我正在开发一个实时消息应用程序。我已经得到了用户信息存储在firebase实时数据库。但无法将它们填充回listview。我有一个名为displaymessages()的方法,我想从数据库中提取所有消息并填充到listview中。出于某种原因,它没有填充视图,我也不知道为什么。任何能帮忙的人。

public class ChatRoom extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private EditText input;
    private FloatingActionButton fab;
    private FirebaseListAdapter<Comment> adapter;
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_room);
        fab = findViewById(R.id.fab);
        input = findViewById(R.id.input);
        mAuth = FirebaseAuth.getInstance();
        final String userID = mAuth.getUid();
        assert userID != null;
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User").child(userID);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                User user = snapshot.getValue(User.class);
                assert user != null;
                String userName = user.getUserName();
                createMessage(userName);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(ChatRoom.this, "Message is empty!", Toast.LENGTH_SHORT).show();
            }
        });
        displayMessages();
    }

    public void createMessage(final String userName) {
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (input.getText().toString().isEmpty()) {
                    Toast.makeText(ChatRoom.this, "Message is empty!", Toast.LENGTH_SHORT).show();
                } else {
                    Date date = new Date();
                    String currentDateTime = dateFormat.format(date);
                    mAuth = FirebaseAuth.getInstance();
                    final String userID = mAuth.getUid();
                    Comment comment = new Comment(input.getText().toString(), userName,userID,currentDateTime);
                    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
                    databaseReference.child("Chat").push().setValue(comment).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(ChatRoom.this, "Message Saved", Toast.LENGTH_SHORT).show();
                                input.setText("");
                                displayMessages();
                            }
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(ChatRoom.this, "Error Occurred: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    //I am able to write new messages to firebase realtime database
    //I need to fix this method and populate the list with the messages from the database
    public void displayMessages() {
        Query query = FirebaseDatabase.getInstance().getReference().child("Chat");
        ListView listOfMessages = findViewById(R.id.list_of_messages);
        FirebaseListOptions<Comment> options =
                new FirebaseListOptions.Builder<Comment>()
                        .setQuery(query, Comment.class)
                        .setLayout(R.layout.message)
                        .build();
        adapter = new FirebaseListAdapter<Comment>(options){
            @Override
            protected void populateView(@NotNull View v, @NotNull Comment model, int position) {
                TextView messageText = v.findViewById(R.id.message_text);
                TextView messageUser = v.findViewById(R.id.message_user);
                TextView messageTime = v.findViewById(R.id.message_time);
                messageText.setText(model.getMessageText());
                messageUser.setText(model.getMessageUser());
                messageTime.setText(model.getDateTime());
            }
        };
        listOfMessages.setAdapter(adapter);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题