【发布时间】:2026-02-15 23:10:01
【问题描述】:
我正在尝试使用 firebase 数据库制作聊天应用。
但我有一个错误。红色下划线出现在 displaychat 方法中。以灰色显示聊天方法处于非活动状态。
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 扩展类 FirebaseIndexListAdapter 中声明的对象
构造函数
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;
}
聊天.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" />
项目.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" />
【问题讨论】:
-
也许您可能想使用
FirebaseListAdapter而不是FirebaseIndexListAdapter? -
是的...我太笨了:(非常感谢您的宝贵时间
标签: android firebase firebase-realtime-database firebaseui