本文介绍了FirebaseListAdapter<>无法运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注教程,以创建聊天应用.这是我的displayChatMessages()方法:

I am following this tutorial to create a chat app. This is my displayChatMessages() method:

ListView listOfMessages = (ListView)findViewById(R.id.list_of_messages);

    adapter = new FirebaseListAdapter<ChatMessages>(this, ChatMessages.class, R.layout.message, FirebaseDatabase.getInstance().getReference()) {
        @Override
        protected void populateView(View v, ChatMessages model, int position) {
            // Get references to the views of message.xml
            TextView messageText = (TextView)v.findViewById(R.id.message_text);
            TextView messageTime = (TextView)v.findViewById(R.id.message_time);

            // Set their text
            messageText.setText(model.getMessageBody());
            // Format the date before showing it
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
        }
    };

    listOfMessages.setAdapter(adapter);

但是我在这部分上有一个红色下划线:

But I get a red underline at this part:

(this, ChatMessages.class, R.layout.message, FirebaseDatabase.getInstance().getReference())

Android Studio说

Android Studio says

更新:这是错误消息:

推荐答案

感谢完整的错误消息-这很有帮助.基于此,我认为这是您需要做的.

Thanks for complete error message - this helps greatly.Based on that, I think this is what you need to do.

//Suppose you want to retrieve "chats" in your Firebase DB:
Query query = FirebaseDatabase.getInstance().getReference().child("chats");
//The error said the constructor expected FirebaseListOptions - here you create them:
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
                    .setQuery(query, ChatMessages.class)
                     .setLayout(android.R.layout.message)
                    .build();
    //Finally you pass them to the constructor here:
 adapter = new FirebaseListAdapter<ChatMessages>(options){
    @Override
    protected void populateView(View v, ChatMessages model, int position) {
        // Get references to the views of message.xml
        TextView messageText = (TextView)v.findViewById(R.id.message_text);
        TextView messageTime = (TextView)v.findViewById(R.id.message_time);

        // Set their text
        messageText.setText(model.getMessageBody());
        // Format the date before showing it
        messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
    }
 };

这是文章,其中存在相同的问题

Here is an article where the same issue was encountered.

这篇关于FirebaseListAdapter&lt;&gt;无法运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:11
查看更多