FirebaseListAdapter<>不工作

时间:2017-12-07 08:48:14

标签: android firebase firebase-realtime-database firebaseui

我正在关注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 Studio说

  Firebaselistadapter中的

Firebaselistadapter()无法应用于...

更新:这是错误消息:

  

错误:(76,19)错误:类FirebaseListAdapter中的构造函数FirebaseListAdapter无法应用于给定类型;   必需:FirebaseListOptions   发现:MainActivity,Class,int,DatabaseReference   原因:实际和正式的参数列表长度不同   其中T是一个类型变量:   T扩展了类FirebaseListAdapter中声明的Object

2 个答案:

答案 0 :(得分:3)

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

//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

答案 1 :(得分:0)

如错误所示,您需要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

 }
};