FirebaseListAdapter<> 不工作

FirebaseListAdapter<> Not Working

我正在按照 this 教程创建聊天应用程序。这是我的 displayChatMessages() 方法:

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

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

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

Android 工作室说

Firebaselistadapter() in Firebaselistadapter cannot be applied to...

更新: 这是错误信息:

Error:(76, 19) error: constructor FirebaseListAdapter in class FirebaseListAdapter cannot be applied to given types; required: FirebaseListOptions found: MainActivity,Class,int,DatabaseReference reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class FirebaseListAdapter

如错误提示,您需要FirebaseListOptions,请使用:

Query query=FirebaseDatabase.getInstance().getReference().orderByChild("name").equalTo(name);
FirebaseListOptions<ChatMessages> options = new FirebaseListOptions.Builder<ChatMessages>()
    .setQuery(query, ChatMessages.class)
    .build();

FirebaseListAdapter<ChatMessages> adapter = new FirebaseListAdapter<ChatMessages>(options) {
@Override
protected void populateView(View v, ChatMessages model, int position) {
    //code here

 }
};

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

//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()));
    }
 };

这里是 article 遇到同样问题的地方。