Firebase数据库错误“构造函数无法应用于给定类型”错误

时间:2017-06-12 20:51:55

标签: android firebase firebase-realtime-database firebaseui

我正在尝试与firebase数据库建立聊天应用。

但我有一个错误。显示方式中出现红色下划线。显示聊天方法处于非活动状态,灰色。

    public class MessagePage extends AppCompatActivity {

    private static final int RC_SIGN_IN = 1;

    private FirebaseListAdapter<Message> adapter;
    RelativeLayout chat;
    Button sendChatMessageBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat);

        chat = (RelativeLayout) findViewById(R.id.chat_page);
        sendChatMessageBtn = (Button) findViewById(R.id.chat_send_btn);
        sendChatMessageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText input = (EditText) findViewById(R.id.chat_edit_field);

    // Код, который формирует объект для отправки в БД
                FirebaseDatabase.getInstance().getReference().push()
                        .setValue(new Message(input.getText().toString(),
                                FirebaseAuth.getInstance().getCurrentUser().getEmail()));

                input.setText("");
            }
        });
    }

    private void displayChat(){
        ListView listMessages = (ListView) findViewById(R.id.chat_list_messages);
        adapter = new FirebaseIndexListAdapter<Message>(
                this,
                Message.class,
                R.id.chat_list_messages,
                FirebaseDatabase.getInstance().getReference()
        ) {
            @Override
            protected void populateView(View v, Message model, int position) {

                TextView textMessage, author, timeMessage;
                textMessage = (TextView) findViewById(R.id.tvMessage);
                author = (TextView) findViewById(R.id.tvUser);
                timeMessage = (TextView) findViewById(R.id.tvTime);

                textMessage.setText(model.getTextMessage());
                author.setText(model.getAuthor());
                timeMessage.setText(DateFormat.format("dd-MM-yyyy", model.getTimeMessage()));

            }
        };

        listMessages.setAdapter(adapter);
    }


}

错误消息

  

错误:(54,19)错误:类中的构造函数FirebaseIndexListAdapter   FirebaseIndexListAdapter不能应用于给定的类型;   必需:Activity,Class,int,Query,Query found:   MessagePage,Class,int,DatabaseReference原因:实际和   形式参数列表的长度不同,其中T是一个类型变量:T   extends类FirebaseIndexListAdapter中声明的Object

构造

public class Message {

private String textMessage;
private String author;
private long timeMessage;

public Message(String author, String textMessage) {
    this.author = author;
    this.textMessage = textMessage;
    timeMessage = new Date().getTime();
}

public Message() {
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getTextMessage() {
    return textMessage;
}

public void setTextMessage(String textMessage) {
    this.textMessage = textMessage;
}

public long getTimeMessage() {
    return timeMessage;
}

public void setTimeMessage(long timeMessage) {
    this.timeMessage = timeMessage;
}

chat.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/chat_page"
android:orientation="vertical"
>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:id="@+id/chat_edit_field"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />



    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chat_send_btn"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="18dp" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:id="@+id/chat_list_messages" />

item.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message_item"
android:orientation="vertical"
>

<TextView
    android:text="TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvUser" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvTime"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="44dp" />

<TextView
    android:text="TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvMessage"
    android:layout_centerVertical="true" />

1 个答案:

答案 0 :(得分:0)

这对我有帮助

 private void displayChatMessage(){
        ListView listOfMessage = (ListView) findViewById ( R.id.list_of_message );
        Query query = FirebaseDatabase.getInstance().getReference().child("chats");

        FirebaseListOptions<ChatMessage> options =
                new FirebaseListOptions.Builder<ChatMessage>()
                        .setQuery(query, ChatMessage.class)
                        .setLayout(android.R.layout.simple_list_item_1)
                        .build();
        adapter = new FirebaseListAdapter<ChatMessage>(options){
            @Override
            protected void populateView(@NonNull View v, @NonNull ChatMessage model, int position) {
                TextView messageText, messageUser, messageTime;
                messageText = v.findViewById ( R.id.message_text );
                messageUser = v.findViewById ( R.id.message_user );
                messageTime = v.findViewById ( R.id.message_time );

                messageText.setText ( model.getMessageText () );
                messageUser.setText ( model.getMessageUser () );
                messageTime.setText ( DateFormat.format("dd-MM-yyyy(HH:mm:ss)", model.getMessageTime ()) );
            }
        };
        listOfMessage.setAdapter ( adapter );
    }