代码之家  ›  专栏  ›  技术社区  ›  Yash

firebase数据库+arrayadapter在我的android应用程序中没有突然更新

  •  0
  • Yash  · 技术社区  · 6 年前

    我是个新手,最近才知道 火场 是的。实际上,我有 列表视图+阵列适配器 ,我正在显示候选人名单。当我启动应用程序时,当前用户的默认数据(图像+名称)将输入到列表视图中,并显示为名称+图像(由于我尚未应用,因此为空)。因此,当我单击浮动操作按钮时,它将更新当前用户的图像,并返回到主活动(这里实现了listview+arrayadapter)。另外,在这个uri也在 firebase数据库 正确地。

    但是 问题是 那张照片还是空白的- https://i.stack.imgur.com/8nywY.jpg )当我按Home键回来或注销并再次登录时,就会显示图像(图像- https://i.stack.imgur.com/3X39v.jpg )中。不是的 同步 或者突然改变。甚至,我在用 ChildEventListener arrayAdapter.notifyDataSetChanged() 但是有突然的变化。

    这是我的主要活动-

        public class MainActivity extends AppCompatActivity {
        private String currentUserId;
        private ArrayList<EachPerson> mEachPersonList;
        private ArrayAdapter<EachPerson> arrayAdapter;
        private FloatingActionButton fab_addImage;
        public static final int RC_SIGN_IN = 1;
        private static final int RC_PHOTO_PICKER =  2;
        static final int RC_IMAGE_CAPTURE = 3;
    
        private ImageView cardImage;
        private FirebaseAuth mFirebaseAuth;
        private FirebaseDatabase mFirebaseDatabase;
        private DatabaseReference mDatabaseReference;
        private ChildEventListener mChildEventListener;
        private FirebaseAuth.AuthStateListener mAuthStateListener;
        private FirebaseStorage mFirebaseStorage;
        private StorageReference mStorageReference;
        private SwipeFlingAdapterView flingContainer;
        private String mUsername;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mFirebaseAuth = FirebaseAuth.getInstance();
            currentUserId = mFirebaseAuth.getCurrentUser().getUid();
            mFirebaseDatabase = FirebaseDatabase.getInstance();
            mDatabaseReference = mFirebaseDatabase.getReference().child("Users");
            mFirebaseStorage = FirebaseStorage.getInstance();
            mStorageReference = mFirebaseStorage.getReference().child("poll_images");
    
            cardImage = findViewById(R.id.img_person_cardview);
            onSignedInInitialize();
            fab_addImage = findViewById(R.id.fab_add);
            mEachPersonList = new ArrayList<EachPerson>();
            arrayAdapter = new CustomArrayAdapter(this, R.layout.item, mEachPersonList );
            flingContainer = (SwipeFlingAdapterView)findViewById(R.id.frame);
            flingContainer.setAdapter(arrayAdapter);
            flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
                @Override
                public void removeFirstObjectInAdapter() {}
    
                @Override
                public void onLeftCardExit(Object dataObject) {}
    
                @Override
                public void onRightCardExit(Object dataObject) {}
    
                @Override
                public void onAdapterAboutToEmpty(int itemsInAdapter) {}
    
                @Override
                public void onScroll(float scrollProgressPercent) {}});
    
            fab_addImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Intent.ACTION_PICK);
                    intent.setType("image/*");
                    startActivityForResult(Intent.createChooser(intent,"Complete Action Using"),RC_PHOTO_PICKER);}
            });}
    
        private void onSignedInInitialize() {
            attachDatabaseReadListener();}
    
        private void attachDatabaseReadListener() {
            if (mChildEventListener == null) {
                mChildEventListener = new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        String pollImageUrl = "default";
                        if (!dataSnapshot.child("pollImages").getValue().equals("default")) {
                            pollImageUrl =dataSnapshot.child("pollImages").getValue().toString();
                        }
                        EachPerson eachPerson = new EachPerson(dataSnapshot.getKey(), dataSnapshot.child("name").getValue().toString(), pollImageUrl,0);
                        mEachPersonList.add(eachPerson);
                        arrayAdapter.notifyDataSetChanged();
                    }
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
                    public void onChildRemoved(DataSnapshot dataSnapshot) {}
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
                    public void onCancelled(DatabaseError databaseError) {}
                };
                mDatabaseReference.addChildEventListener(mChildEventListener);
            }
    }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RC_SIGN_IN) {
                if (resultCode == RESULT_OK) {
                    Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show();
                } else if (resultCode == RESULT_CANCELED) {
                    Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
            else if(requestCode==RC_PHOTO_PICKER && resultCode==RESULT_OK){
                Uri imageUri = data.getData(); //image will be return back as the uri
                final StorageReference photoRef = mStorageReference.child(imageUri.getLastPathSegment());
                UploadTask uploadTask = photoRef.putFile(imageUri);
                uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                    @Override
                   public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                        if (!task.isSuccessful()) {
                            throw task.getException();
                       }
                       return photoRef.getDownloadUrl();
                   }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                   @Override
                   public void onComplete(@NonNull Task<Uri> task) {
                       if (task.isSuccessful()) {
                           Uri downloadUri = task.getResult();
    
                           mDatabaseReference = mFirebaseDatabase.getReference().child("Users").child(currentUserId).child("pollImages");
                           mDatabaseReference.setValue(downloadUri.toString());
                           arrayAdapter.notifyDataSetChanged();
                        } else {// Handle failures}
                    }
                });
            }
            attachDatabaseReadListener();
        }
    
        private void onSignedOutCleanup() {
            arrayAdapter.clear();
            detachDatabaseReadListener();}
    
        private void detachDatabaseReadListener() {
            if (mChildEventListener != null) {
                mDatabaseReference.removeEventListener(mChildEventListener);
                mChildEventListener = null;
            }}
    }
    
    0 回复  |  直到 6 年前