Main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:id="@+id/activity_main"
tools:context="com.example.syafiq.mychatapp.MainActivity">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/ic_send"
android:id="@+id/fab"
android:tint="@android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
app:fabSize="mini"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/fab"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
>
<EditText
android:id="@+id/messageinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/fab"
android:layout_centerHorizontal="true"
android:hint="Message..." />
</android.support.design.widget.TextInputLayout>
<ListView
android:id="@+id/list_of_message"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/fab"
android:dividerHeight="16dp"
android:divider="@android:color/transparent"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
Maincode
private static int SIGN_IN_REQUEST_CODE =1;
//private List<ChatMessage> list = new ArrayList<ChatMessage>();
FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_main;
FloatingActionButton fab;
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference myRef = db.getReference("message");
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.menu_signout)
{
AuthUI
.getInstance()
.signOut(this)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
Snackbar
.make(
activity_main,
"You have been signed out.",
Snackbar.LENGTH_SHORT
).show()
;
finish();
}
}
)
;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==SIGN_IN_REQUEST_CODE)
{
if (resultCode==RESULT_OK)
{
Snackbar
.make(
activity_main,
"Successfully signed in!",
Snackbar.LENGTH_SHORT
).show()
;
displayChatMessage();
}
else
{
Snackbar
.make(
activity_main,
"We couldn't sign you in. Please try again lter!",
Snackbar.LENGTH_SHORT
).show()
;
finish();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main = (RelativeLayout) findViewById(R.id.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab
.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v)
{
EditText input = (EditText) findViewById(R.id.messageinput);
FirebaseDatabase
.getInstance()
.getReference()
.push()
.setValue(
new ChatMessage(
input.getText().toString(),
FirebaseAuth.getInstance().getCurrentUser().getEmail()
)
)
;
input.setText("");
displayChatMessage();
}
}
)
;
if (FirebaseAuth.getInstance().getCurrentUser()== null)
{
startActivityForResult(
AuthUI
.getInstance()
.createSignInIntentBuilder()
.build(),
SIGN_IN_REQUEST_CODE
);
}
else
{
Snackbar
.make(
activity_main,
"Welcome " + FirebaseAuth
.getInstance()
.getCurrentUser()
.getEmail(),
Snackbar.LENGTH_SHORT
).show()
;
//displayChatMessage();
}
}
DisplaychatMessage()函数显示我的聊天消息
private void displayChatMessage() {
Query query = FirebaseDatabase.getInstance().getReference().child("Chats");
ListView listofmsgs = (ListView) findViewById(R.id.list_of_message);
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions
.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.setLayout(R.layout.list_item)
.build()
;
//adapter.startListening();
Log.d("ErrorCheck", "1");
adapter = new FirebaseListAdapter<ChatMessage>(options) {
@Override protected void populateView(View v, ChatMessage model, int position) {
//ChatMessage cm = (ChatMessage) model;
TextView messageText, messageUser, messageTime;
messageText = (TextView) v.findViewById(R.id.messageinput);
messageUser = (TextView) v.findViewById(R.id.message_user);
messageTime = (TextView) v.findViewById(R.id.message_time);
messageText.setText(model.getMessageText().toString());
messageUser.setText(model.getMessageUser());
messageTime
.setText(
android.text.format.DateFormat.format(
"dd-mm-yyyy (HH:mm:ss)",
model.getMessageTime()
)
)
;
Log.d("ErrorCheck", "2");
}
};
listofmsgs.setAdapter(adapter);
adapter.startListening();
}
嗨,伙计们,我这样做了,但它似乎没有任何东西出现在我的APP上。但是当我按下发送时,在我的数据库中,我的聊天会出现在那里,但它再次出现在我的聊天应用中。我做了一个调试日志。错误检查1和2以查看代码的结束位置。当我检查时,看起来调试日志只显示到ErrorCheck 1并且不显示显示2.我如何解决这个问题?
答案 0 :(得分:0)
您已将ErrorCheck 2放入名为anonymous class的内容中。这就是为什么调用displayChatMessage()
只会记录ErrorCheck 1.您定义的匿名FirebaseListAdapter类中的代码只会在调用populateView()
方法时运行。你不打电话给那个方法。您致电adapter.startListening();
在您看到ErrorCheck 2已登录之前,某处某处需要拨打adapter.populateView()
。
你可能不想在这里打电话。你可以在这里调用它只是为了测试,但是你应该追踪应该调用它的东西。
根据docs about FirebaseListAdapter
此类是使用Firebase位置支持Android ListView的通用方法。它处理给定Firebase位置的所有子事件。它将收到的数据编组到给定的类类型中。扩展此类并提供populateView的实现,该实现将获得列表项mLayout的实例以及包含数据的类的实例。只需按照您的喜好填充视图,此类将在数据更改时处理更新列表。
因此,尝试更改数据并查看是否可以记录ErrorCheck 2