FirebaseListAdapter<> Not Working [duplicate]

自古美人都是妖i 提交于 2020-01-11 13:04:08

问题


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 says

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

UPDATE: This is the error message:

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


回答1:


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.




回答2:


As the error says, you need FirebaseListOptions, please use this:

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

 }
};


来源:https://stackoverflow.com/questions/47690974/firebaselistadapter-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!