我正在构建基于firebase数据库的实时聊天应用 所以我的问题是,每次应用程序调用oncreat方法时,我都会调用displaychatmessages()方法,问题是我的数据库中的所有消息都显示在listview上。 我的问题,当数据库变大时,应用程序可能会遇到一些问题?经过一些月或几年?我的意思是它会在开始时间处理很多数据
作为一种解决方案,我试图在以后存储旧邮件并稍后加载它们,应用程序只会加载新添加的子邮件?但我不知道如何开始这个,如果一个人可以帮助我一个好主意?
我的旧displayChatMessages方法: 关于新的想法?
private void displayChatMessages() {
RecyclerView recycler = (RecyclerView)findViewById(R.id.myRv);
LinearLayoutManager layoutmang = new LinearLayoutManager(this);
layoutmang.setStackFromEnd(true);
recycler.setLayoutManager(layoutmang);
mAdapter = new FirebaseRecyclerAdapter<ChatMessage, RecyclerView.ViewHolder>(
ChatMessage.class, R.layout.activity_main, RecyclerView.ViewHolder.class, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateViewHolder(final RecyclerView.ViewHolder viewHolder, final ChatMessage user,
final int position) {
int y=0;
ChatMessage user2;
if (position>1){
user2 = getItem(position - 1);
}else{
user2 = getItem(position );
}
ChatMessage ori2 = getItem(position );
if (user.getMessageUser().matches(FirebaseAuth.getInstance().getCurrentUser().getEmail())) {
if (user2.getMessageFULL()<ori2.getMessageFULL()){
y=1;
populateType1((HolderMe) viewHolder, user, 1);
}else{
populateType1((HolderMe) viewHolder, user, 0);
}
}else if (!user.getMessageUser().matches(FirebaseAuth.getInstance().getCurrentUser().getEmail())){
if (user2.getMessageFULL()<ori2.getMessageFULL()){
populateType2((HolderYou) viewHolder, user, 1,FirebaseAuth.getInstance().getCurrentUser().getEmail());
}else{
populateType2((HolderYou) viewHolder, user, 0,FirebaseAuth.getInstance().getCurrentUser().getEmail());
}
}
if(user.getMessageTime()< new Date().getTime()) {
HolderDate vh3 = (HolderDate) viewHolder;
populateType3(vh3, user, position);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.e("ViewT",viewType+"");
if (viewType == 1) {
View userType1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_holder_me, parent, false);
return new HolderMe(userType1);
} else{
View userType2 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_holder_you, parent, false);
return new HolderYou(userType2);
}
}
@Override
public int getItemViewType(int position) {
ChatMessage user = getItem(position);
String s = FirebaseAuth.getInstance().getCurrentUser().getEmail();
if (s.equals(user.getMessageUser())) {
return 1;
} else {
return 2;
}
}
private void populateType1(HolderMe v, ChatMessage model, int position) {
v.getTimeView().setText(DateFormat.format("HH:mm",model.getMessageTime()));
if (position==1) {
v.getFullDateView().setText(DateFormat.format("dd-MM-yyyy", model.getMessageTime()));
v.getFullDateView().setVisibility(View.VISIBLE);
}
v.getTextv().setText(model.getMessageText());
}
private void populateType3(HolderDate vcv, ChatMessage model, int position) {
vcv.getDate().setText(DateFormat.format("dd-MM-yyyy",model.getMessageTime()));
}
private void toaa1( String text) {
Toast.makeText(ChatActivity.this,text,Toast.LENGTH_LONG).show();
}
private void populateType2(HolderYou vv, ChatMessage model, int position,String username) {
vv.getTimeView().setText(DateFormat.format("HH:mm",model.getMessageTime()));
if (position==1) {
vv.getFullDateView().setVisibility(View.VISIBLE);
vv.getFullDateView().setText(DateFormat.format("dd-MM-yyyy", model.getMessageTime()));
}
vv.getTextv().setText(model.getMessageText());
if (username.matches("test2@test2.com")) {
vv.getImageView().setBackgroundResource(R.drawable.userm);
} else {
vv.getImageView().setBackgroundResource(R.drawable.usera);
}
}
};
recycler.setAdapter(mAdapter);
}
答案 0 :(得分:0)
显示聊天应用中的所有消息确实会随着消息数量的增加而出现问题。相反,您可以使用Firebase数据库查询仅显示最新消息。例如:
DatabaseReference messages = FirebaseDatabase.getInstance().getReference();
Query recent = messages.orderByKey().limitToLast(100);
然后在适配器中使用它:
mAdapter = new FirebaseRecyclerAdapter<ChatMessage, RecyclerView.ViewHolder>(
ChatMessage.class,
R.layout.activity_main,
RecyclerView.ViewHolder.class,
recent) {