android - 尝试在firebase上添加新孩子时android应用程序冻结
问题描述
我正在使用 firebase 制作聊天应用程序。我添加了子事件 onChildAdded 侦听器。当我第一次打开活动时,它会检索已经正确存在的数据,但是当我尝试在活动打开时添加子节点时,应用程序会冻结。请帮忙。这是我的活动代码:
public class ChatActivity extends AppCompatActivity {
DatabaseReference ref,ref1;
TextView t;
EditText e;
ImageButton b;
String contact,user;
ArrayList<Chat> chatList;
ListView listView;
ChatAdapter chatAdapter;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
chatList = new ArrayList<>();
Intent i = getIntent();
contact = i.getStringExtra("contact");
user = i.getStringExtra("user");
t = findViewById(R.id.textView3);
e = findViewById(R.id.editText8);
b = findViewById(R.id.imageButton);
listView = findViewById(R.id.listView2);
t.setText(" " + contact);
ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (!dataSnapshot.exists()) {
return;
}
Toast.makeText(getApplicationContext(), "onChildAdded of ref", Toast.LENGTH_LONG).show();
while(!(dataSnapshot.hasChild("type") && dataSnapshot.hasChild("msg") && dataSnapshot.hasChild("time"))){}
chatList.add(new Chat(dataSnapshot.child("type").getValue(Integer.class), dataSnapshot.child("msg").getValue(String.class), dataSnapshot.child("time").getValue(Long.class)));
if(chatAdapter!=null)
chatAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
msg = e.getText().toString();
ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
ref1 = FirebaseDatabase.getInstance().getReference().child("Messages").child(contact).child(user);
ref = ref.push();
ref1 = ref1.push();
ref.child("type").setValue(1);
ref1.child("type").setValue(2);
ref.child("msg").setValue(msg);
ref1.child("msg").setValue(msg);
ref.child("time").setValue(ServerValue.TIMESTAMP);
ref1.child("time").setValue(ServerValue.TIMESTAMP);
}
});
chatAdapter = new ChatAdapter(this, chatList);
listView.setAdapter(chatAdapter);
}
}
这是 chatAdapter 和 Chat 类代码:
public class ContactAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contactList = new ArrayList<>();
public ContactAdapter(@NonNull Context context, ArrayList<Contact> list) {
super(context, 0 , list);
this.context = context;
contactList = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(context).inflate(R.layout.contact_item,parent,false);
final Contact currentContact = contactList.get(position);
TextView name = (TextView) listItem.findViewById(R.id.textView);
name.setText(" " + currentContact.getUser());
TextView release = (TextView) listItem.findViewById(R.id.textView2);
release.setText(" " + currentContact.getLastMsg());
return listItem;
}
}
public class Chat {
private int type;
private String msg;
private long time;
public Chat(int type,String msg,long time){this.type = type; this.msg = msg; this.time = time;}
public int getType(){return type;}
public void setType(int type){this.type = type;}
public String getMsg(){return msg;}
public void setMsg(String msg){this.msg = msg;}
public long getTime(){return time;}
public void setTime(long time){this.time = time;}
}
这就是我的 Firebase 数据的样子:
{
"Messages" : {
"pranavk28" : {
"sunilk26" : {
"-LFRGvG86BpKNYBONRKX" : {
"msg" : "hii",
"time" : 1529482819019,
"type" : 1
},
"-LFRH3nTMYv-QcqXkFFb" : {
"msg" : "how are you?",
"time" : 1529482858064,
"type" : 1
},
"-LFRIWJvmACC61niuN07" : {
"msg" : "How was your day?",
"time" : 1529483237050,
"type" : 1
}
}
},
"sunilk26" : {
"pranavk28" : {
"-LFRGvG9JJcpprSKz8YB" : {
"msg" : "hii",
"time" : 1529482819020,
"type" : 2
},
"-LFRH3nTMYv-QcqXkFFc" : {
"msg" : "how are you?",
"time" : 1529482858064,
"type" : 2
},
"-LFRIWJwEJ_ypA1zeBXt" : {
"msg" : "How was your day?",
"time" : 1529483237051,
"type" : 2
}
}
}
},
"Users" : {
"pranavk28" : {
"email" : "pranavk28@gmail.com",
"name" : "Pranav Bhardwaj",
"password" : "priyamkr",
"username" : "pranavk28"
},
"rockykbc" : {
"email" : "rocky6@hotmail.com",
"name" : "Ravi Kumar",
"password" : "neetukbc",
"username" : "rockykbc"
},
"sunilk26" : {
"email" : "thakur@gmail.com",
"name" : "D K Thakur",
"password" : "pass1234",
"username" : "sunilk26"
}
}
}
解决方案
推荐阅读
- flutter - 单击按钮时显示小部件(Dart,Flutter)
- javascript - XMLHttpRequest() 以什么顺序执行
- google-cloud-platform - Dataflow Flex 模板作业已排队
- python-3.x - Python中的Countif函数在每个单元格中循环
- wso2 - 如何在 WSO2 中打开和编辑 JAR 包?
- python - Django“'str'对象没有属性'decode'”错误
- plsql - 如何在 DBeaver/DBVisualizer 中编写 DB2 pl/sql 脚本
- nginx - Kubernetes Ingress 未正确找到/公开应用程序
- amazon-web-services - 如何获取具有特定模式的aws存储桶上的文件列表
- javascript - 设置双精度数组值