0

MainActivity图像不是RecyclerView的ImageView的检索从火力地堡

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = "MainActivity"; 

    public static final String ANONYMOUS = "anonymous"; 
    public static final int RC_SIGN_IN = 1; 
    private static final int RC_PHOTO_PICKER = 2; 
    private String mUsername; 

    // Firebase instance variables 
    private FirebaseDatabase mFirebaseDatabase; 
    private DatabaseReference mMessagesDatabaseReference; 
    private ChildEventListener mChildEventListener; 
    private FirebaseAuth mFirebaseAuth; 
    private FirebaseAuth.AuthStateListener mAuthStateListener; 
    private FirebaseStorage mFirebaseStorage; 
    private StorageReference mChatPhotosStorageReference; 
    private FirebaseRemoteConfig mFirebaseRemoteConfig; 

    private RecyclerView recyclerView; 
    private FloatingActionButton floatingActionButton; 

    PhotosAdapter contactsAdapter; 
    List<Photos> contactList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mUsername = ANONYMOUS; 
     recyclerView=(RecyclerView)findViewById(R.id.recyclerview); 
     floatingActionButton=(FloatingActionButton)findViewById(R.id.floatingactionbutton); 
     contactList = new ArrayList(); 
     contactsAdapter=new PhotosAdapter(contactList,getApplicationContext()); 

     // Initialize Firebase components 
     mFirebaseDatabase = FirebaseDatabase.getInstance(); 
     mFirebaseAuth = FirebaseAuth.getInstance(); 
     mFirebaseStorage = FirebaseStorage.getInstance(); 
     mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); 


     mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); 
     mChatPhotosStorageReference = mFirebaseStorage.getReference().child("chat_photos"); 

     mAuthStateListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       FirebaseUser user = firebaseAuth.getCurrentUser(); 
       if (user != null) { 
        // User is signed in 
        onSignedInInitialize(user.getDisplayName()); 
       } else { 
        // User is signed out 
        onSignedOutCleanup(); 
        startActivityForResult(
          AuthUI.getInstance() 
            .createSignInIntentBuilder() 
            .setIsSmartLockEnabled(false) 
            .setProviders(
              AuthUI.EMAIL_PROVIDER, 
              AuthUI.GOOGLE_PROVIDER) 
            .build(), 
          RC_SIGN_IN); 
       } 
      } 
     }; 

     recyclerView.setAdapter(contactsAdapter); 

     recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),5)); 


     floatingActionButton.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, "Complete action using"), RC_PHOTO_PICKER); 

      } 
     }); 
    } 


    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RC_SIGN_IN) { 
      if (resultCode == RESULT_OK) { 
       // Sign-in succeeded, set up the UI 
       Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // Sign in was canceled by the user, finish the activity 
       Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
     }else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 

      // Get a reference to store file at chat_photos/<FILENAME> 
      StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); 

      // Upload file to Firebase Storage 
      photoRef.putFile(selectedImageUri) 
        .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() { 
         public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
          // When the image has successfully uploaded, we get its download URL 
          Uri downloadUrl = taskSnapshot.getDownloadUrl(); 

          // Set the download URL to the message box, so that the user can send it to the database 
          Photos friendlyMessage = new Photos(downloadUrl.toString()); 
          mMessagesDatabaseReference.push().setValue(friendlyMessage); 
         } 
        }); 
     } 
    } 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     mFirebaseAuth.addAuthStateListener(mAuthStateListener); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (mAuthStateListener != null) { 
      mFirebaseAuth.removeAuthStateListener(mAuthStateListener); 
     } 
    } 

     private void onSignedInInitialize(String username) { 
     mUsername = username; 
     attachDatabaseReadListener(); 
    } 

    private void onSignedOutCleanup() { 
     mUsername = ANONYMOUS; 

    } 

    private void attachDatabaseReadListener() { 
     if (mChildEventListener == null) { 
      mChildEventListener = new ChildEventListener() { 
       @Override 
       public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
        Photos friendlyMessage = dataSnapshot.getValue(Photos.class); 
        contactsAdapter.add(friendlyMessage); 
       } 

       public void onChildChanged(DataSnapshot dataSnapshot, String s) {} 
       public void onChildRemoved(DataSnapshot dataSnapshot) {} 
       public void onChildMoved(DataSnapshot dataSnapshot, String s) {} 
       public void onCancelled(DatabaseError databaseError) {} 
      }; 
      mMessagesDatabaseReference.addChildEventListener(mChildEventListener); 
     } 
    } 
} 

PhotosAdapter

public class PhotosAdapter extends RecyclerView.Adapter<PhotosAdapter.MyViewHolder> { 

    private List<Photos> photosList; 
    private Context context; 
    public static final String Position="AdapterPosition"; 

    public PhotosAdapter(List<Photos> contactsList, Context context) { 
     this.photosList = contactsList; 
     this.context = context; 
    } 

    @Override 
    public PhotosAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(context).inflate(R.layout.list_item, null); 

     return new MyViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(PhotosAdapter.MyViewHolder holder, int position) { 
     Photos contacts = photosList.get(position); 


     Glide.with(context).load(contacts.getPhotoUrl()).into(holder.contactimageView); 

    } 

    @Override 
    public int getItemCount() { 
     return photosList.size(); 
    } 

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
     public ImageView contactimageView; 
     private final Context context; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      context = itemView.getContext(); 
      contactimageView = (ImageView) itemView.findViewById(R.id.imageview); 
      itemView.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      /* Intent intent = new Intent(context, ChatActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.putExtra(Position,getAdapterPosition()); 
      context.startActivity(intent);*/ 
     } 


     public void add(Photos photos){ 
     photosList.add(photos); 
    } 
} 

照片

public class Photos { 
    private String photoUrl; 

    public Photos() { 
    } 

    public Photos(String photoUrl) { 
    this.photoUrl = photoUrl; 
    } 

    public String getPhotoUrl() { 
    return photoUrl; 
    } 

    public void setPhotoUrl(String photoUrl) { 
    this.photoUrl = photoUrl; 
    } 
} 

这是我的代码。我点击FloatingAction按钮上传图片到Firebase Storage.The图片上传成功,但我无法将图片检索到RecyclerViewImageView。我究竟做错了什么?我也能够在Storage.Stell中看到图像的url,但无法在recyclerview的imageview中检索消息。请帮助.......

+0

当您添加照片photoList? public void add(照片照片){ photosList.add(photos); } –

回答

0

你也可以试试这个: -

yourFragment.isResumed()