使用Firebase上传图片的代码:
Button btnUpload = (Button)findViewById(R.id.btn_upload_image);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY,true);
startActivityForResult(Intent.createChooser(intent,getString(R.string.str_complete_action)),RC_PHOTO_PICKER);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK)
{
pbLoader.setVisibility(View.VISIBLE);
Uri selectedImageUri = data.getData();
StorageReference photoReference = mStorageReference.child(getString(R.string.str_images)+selectedImageUri.getLastPathSegment());
photoReference.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
MessageModelClass messageModelClass = new MessageModelClass();
messageModelClass.setImageUrl(downloadUri.toString());
messageModelClass.setmMessage(null);
messageModelClass.setmUserName(userName);
mDatabaseReference.push().setValue(messageModelClass);
messageAdapter.notifyDataSetChanged();
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG,"onFailure:"+e.getMessage());
}
}).addOnCompleteListener(this, new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
// messageAdapter.notifyDataSetChanged();
pbLoader.setVisibility(View.GONE);
Log.d(TAG,"onComplete:");
}
});
}
在我发送任何文本的过程中发送消息,如果3张图片处于待处理状态,并且我发送了一些文本,那么我会发送一些文本然后文本在recyclerview中显示三次,而它只显示数据在firebase控制台中插入一次。 当我再次重新启动应用程序时,它显示确切的消息。 多次显示文字或图像的原因是什么?
Recyclercode:
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvMessage.setLayoutManager(layoutManager);
messageList = new ArrayList<>();
messageList.clear();
messageAdapter = new MessageAdapter(context, messageList , userName ,confirmationDialogListener);
rvMessage.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
}
@Override
public void onBindViewHolder(final MessageAdapter.MyViewHolder holder, final int position) {
final MessageModelClass messageModelClass = messageModelList.get(position);
if (messageModelClass.getmTimeDate() != null) {
setTimeFormatInUI(messageModelClass.getmTimeDate(), holder);
}
if (messageModelClass.getImageUrl() != null) {
holder.tvMessage.setVisibility(View.GONE);
holder.ivPhoto.setVisibility(View.VISIBLE);
holder.ivPhoto.setTag(messageModelClass.getmKey());
Picasso.with(context).load(messageModelClass.getImageUrl()).into(holder.ivPhoto);
} else {
holder.ivPhoto.setVisibility(View.GONE);
holder.tvMessage.setVisibility(View.VISIBLE);
holder.tvMessage.setTag(messageModelClass.getmKey());
holder.tvMessage.setText(messageModelClass.getmMessage());
}
if(messageModelClass.getmUserName() != null)
{
if(userName.equals(messageModelClass.getmUserName()))
{
holder.tvUserName.setText("You");
}
else
{
holder.tvUserName.setText(messageModelClass.getmUserName());
}
}
}
chileEventListener的代码:
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReference;
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mFirebaseDatabase.getReference().child(getString(R.string.str_messages));
private void attachDatabaseReadListener() {
Log.d(TAG,"attachDatabaseReadListener:");
mChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
MessageModelClass messageModelClass = dataSnapshot.getValue(MessageModelClass.class);
messageModelClass.setmKey(dataSnapshot.getKey());
messageList.add(messageModelClass);
messageAdapter = new MessageAdapter(context, messageList , userName ,confirmationDialogListener);
rvMessage.setAdapter(messageAdapter);
Log.d(TAG,"message list size onChildAdded:"+messageList.size());
// messageAdapter.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) {
}
};
mDatabaseReference.addChildEventListener(mChildEventListener);
}
答案 0 :(得分:0)
如下所示更改onBindViewHolder()。然后它将解决您的问题。
@Override
public void onBindViewHolder(final MessageAdapter.MyViewHolder holder, final int position) {
final MessageModelClass messageModelClass = messageModelList.get(position);
if(messageModelClass !=null)
{
if (messageModelClass.getmTimeDate() != null) {
setTimeFormatInUI(messageModelClass.getmTimeDate(), holder);
}
if (messageModelClass.getImageUrl() != null) {
holder.tvMessage.setVisibility(View.GONE);
holder.ivPhoto.setVisibility(View.VISIBLE);
holder.ivPhoto.setTag(messageModelClass.getmKey());
Picasso.with(context).load(messageModelClass.getImageUrl()).into(holder.ivPhoto);
} else {
holder.ivPhoto.setVisibility(View.GONE);
holder.tvMessage.setVisibility(View.VISIBLE);
holder.tvMessage.setTag(messageModelClass.getmKey());
holder.tvMessage.setText(messageModelClass.getmMessage());
}
if(messageModelClass.getmUserName() != null)
{
if(userName.equals(messageModelClass.getmUserName()))
{
holder.tvUserName.setText("You");
}
else
{
holder.tvUserName.setText(messageModelClass.getmUserName());
}
}
else
{
//set all your holer element to visibility gone
//eg. holder.tvUserName.setVisibility(View.GONE);
}
}
答案 1 :(得分:0)
像这样改变你的代码。因为你需要设置适配器才需要notifyItemInserted再没有设置adpter ..
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
MessageModelClass messageModelClass = dataSnapshot.getValue(MessageModelClass.class);
messageModelClass.setmKey(dataSnapshot.getKey());
messageList.add(messageModelClass);
messageAdapter.notifyItemInserted(messageList.size()-1);
}