我正在尝试使用android中的Recyler视图显示用户发送到实时Firebase数据库的消息,但是即使更新Firebase数据库,我的聊天屏幕仍显示空白屏幕。
这是MainChatActivity.kt文件
.add()
这是我用于回收者视图的Adapter.kt文件
var selector = ".a .b .c ";
var parts = [ ".c2", ".c3" ];
var jq = null;
$.each(parts, function(i, val) {
if (jq == null)
jq = $(selector + val);
else
jq.add(selector + val);
});
jq.hide();
这是用于存储来自firebase Messages.kt类的datasnapshot的消息的类
class MainChatActivity : AppCompatActivity() {
var mDisplayName:String?=null
var databaseref:DatabaseReference?=null
var msgEditText:EditText?=null
var sendButton:ImageButton?=null
var msgRow:RecyclerView?=null
var Adapter:Adapter?=null
var viewManager: RecyclerView.LayoutManager?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_chat)
msgEditText = findViewById(R.id.messageInput)
sendButton=findViewById(R.id.sendButton)
msgRow=findViewById<RecyclerView>(R.id.chat_list_view)
Adapter = Adapter(databaseref,mDisplayName,this@MainChatActivity)
viewManager = LinearLayoutManager(this@MainChatActivity)
msgRow?.apply {
layoutManager=viewManager
adapter=Adapter
itemAnimator=DefaultItemAnimator()
}
}
override fun onResume() {
super.onResume()
databaseref=FirebaseDatabase.getInstance().getReference()
msgEditText?.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
sendMessage(msgEditText?.text.toString())
true
})
sendButton?.setOnClickListener {
sendMessage(msgEditText?.text.toString())
}
}
private fun sendMessage(message: String) {
if (message != "") {
val prefs: SharedPreferences = this@MainChatActivity.getSharedPreferences(RegisterActivity.static.myPrefs, 0)
mDisplayName=prefs.getString("DisaplayName","DisaplayName")
Log.v("displaynamechat",mDisplayName.toString())
if(prefs.getString("DisaplayName",null) =="")
{
mDisplayName="Anonymous"
}
val chat = Messages(message, mDisplayName.toString())
databaseref?.child("messages")?.push()?.setValue(chat)
msgEditText?.setText("")
}
}
public override fun onStart() {
super.onStart()
}
override fun onStop() {
super.onStop()
Adapter?.cleanup()
}
}
activity_main_chat.xml
class Adapter (databaseRef: DatabaseReference?, displayName: String?, context: Context): RecyclerView.Adapter<Adapter.viewHolder>(){
var databaseRef:DatabaseReference?=null
var author:String?=null
var context:Context?=null
lateinit var dataSnap:ArrayList<DataSnapshot>
private val mListener = object : ChildEventListener {
override fun onChildAdded(dataSnapshot: DataSnapshot, s: String?) {
dataSnap.add(dataSnapshot)
notifyDataSetChanged()
}
override fun onChildChanged(dataSnapshot: DataSnapshot, s: String?) {
}
override fun onChildRemoved(dataSnapshot: DataSnapshot) {
}
override fun onChildMoved(dataSnapshot: DataSnapshot, s: String?) {
}
override fun onCancelled(databaseError: DatabaseError) {
}
}
init {
this.databaseRef=databaseRef?.child("messages")
this.author=displayName
this.context=context
this.dataSnap = java.util.ArrayList<DataSnapshot>()
this.databaseRef?.addChildEventListener(this.mListener)
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): viewHolder {
var itemvew= LayoutInflater.from(p0.context)
.inflate(R.layout.chat_msg_row,p0,false)
return viewHolder(itemvew)
}
override fun getItemCount(): Int {
return dataSnap.size
}
override fun onBindViewHolder(p0: viewHolder, p1: Int) {
var gettingcurrentSnap= dataSnap.get(p0.adapterPosition)
var meessageClass = gettingcurrentSnap.getValue(Messages::class.java)
Log.v("flashchat message",meessageClass?.msg)
p0.msgText?.setText(meessageClass?.msg)
if(author =="")
author="Anonymous"
p0.author?.setText(meessageClass?.author)
if (meessageClass?.author == author) {
p0.body?.setBackgroundResource(R.drawable.bubble2)
p0.body?.gravity = Gravity.END
p0.author?.setTextColor(Color.GREEN)
}
else
{
p0.body?.setBackgroundResource(R.drawable.bubble1)
p0.body?.gravity = Gravity.START
p0.author?.setTextColor(Color.BLUE)
}
}
class viewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var msgText: TextView?=null
var author: TextView?=null
var body:LinearLayout?=null
init {
msgText=itemView.findViewById(R.id.message)
author=itemView.findViewById(R.id.author)
body=itemView.findViewById(R.id.singleMessageContainer)
}
}
internal fun cleanup() {
databaseRef?.removeEventListener(mListener)
}
}
chat_msg_row.xml
class Messages {
var author: String=""
var msg: String=""
private constructor() {}
constructor(author: String, text: String) {
this.author = author
this.msg = text
}
}