Search code examples
androidfirebaseandroid-recyclerviewinsert-update

uploading all data of RecyclerView from MainActivity in firebase


i am processing some images and adding the data from the process in an List of Array to RecyclerView. now i want to insert all the data into firebase database by checking if an object from the List is matched to firebase database child then insert the second object into the child of the matched child of firebase one by one. How to achieve that by a single button from main activity rather than the RecyclerView.

here is how i am inserting the data in RecyclerView with ModelClass.

if (fileDirectory.isDirectory()) {
        listCroppedImages.clear();
        EmptyViewCroppedImage.setVisibility(View.GONE);
        RVCroppedImages.setVisibility(View.VISIBLE);
        listCroppedImages.clear();
        String PhotoPath[] = new String[100];
        final String StudentMatric[] = new String[100];
        final String AttendanceRecord[] = new String[100];

        for (int i = 1; i <= fileDirectory.listFiles().length; i++) {
            PhotoPath[i] = croppedImageDirectory + i + ".jpg";

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap croppedimageold = BitmapFactory.decodeFile(PhotoPath[i], options);
            Bitmap croppedimagenew = Bitmap.createScaledBitmap(croppedimageold, 460, 66, true);

            StudentMatric[i] = TextImageProcess(croppedimagenew);
            AttendanceRecord[i]=CircleDetection(croppedimagenew, StudentMatric[i]);
            listCroppedImages.add(new CroppedImageModel(String.valueOf(i), PhotoPath[i], StudentMatric[i], AttendanceRecord[i]));
        }
    } else {
        EmptyViewCroppedImage.setVisibility(View.VISIBLE);
        RVCroppedImages.setVisibility(View.GONE);
    }
 -------------------------
 }
 public String TextImageProcess(Bitmap image){
 ----------------
   return String x;
 };

 public String CircleDetection(Bitmap image, String y){
 ----------------------
   return String z;

 } 

what want is

 btnUploadData.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       for(i=1; i=listCroppedImages.size(); i++)
       if(StudentMatric[i].isequalto(firbasedataRef.getKey())){
           DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child(StudentMatric[i]).child("Attendance Record");
           Map newPost = new HashMap();
           newPost.put("Attendance", Attendance[i]);                         
           current_user_db.updateChildren(newPost);

       }

 });

my problem now how to get the data (Student[i], Attendance[i]) in btnUploadData.setOnclickliastener and match the first element of the List of array with firebase key and to insert the next in the child of the key? i am stuck here for days. any suggestion will be very appriciated. Thanks in advance


Solution

  • i did not realize the answer was that simple.. here is what i did

     if (fileDirectory.isDirectory()) {
            listCroppedImages.clear();
            EmptyViewCroppedImage.setVisibility(View.GONE);
            RVCroppedImages.setVisibility(View.VISIBLE);
            listCroppedImages.clear();
            String PhotoPath[] = new String[100];
            final String StudentMatric[] = new String[100];
            final String AttendanceRecord[] = new String[100];
    
            for (int i = 1; i <= fileDirectory.listFiles().length; i++) {
                PhotoPath[i] = croppedImageDirectory + i + ".jpg";
    
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Bitmap croppedimageold = BitmapFactory.decodeFile(PhotoPath[i], options);
                Bitmap croppedimagenew = Bitmap.createScaledBitmap(croppedimageold, 460, 66, true);
    
                StudentMatric[i] = TextImageProcess(croppedimagenew);
                AttendanceRecord[i] = CircleDetection(croppedimagenew, StudentMatric[i]);
                listCroppedImages.add(new CroppedImageModel(String.valueOf(i), PhotoPath[i], StudentMatric[i], AttendanceRecord[i]));
    
                btnUploadAttendance.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
    
                        for (int x = 1; x <= listCroppedImages.size(); x++) {
                            UploadData(StudentMatric[x], AttendanceRecord[x], x);
                        }
                    }
                });
    
    
            }
        } else {
            EmptyViewCroppedImage.setVisibility(View.VISIBLE);
            RVCroppedImages.setVisibility(View.GONE);
        }
    
        -----------------------------------------------------------------------
    
        public void UploadData(final String StudentMatric, final String AttendanceRecord, final int x) {
             ProgressUploadAttendance.setVisibility(View.VISIBLE);
    
            Query query = StudentsRef.orderByKey().equalTo(StudentMatric);
            query.addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  if (dataSnapshot.exists()) {
                      int progress = x / listCroppedImages.size() * 100;
                      DatabaseReference StudentMatricRef = StudentsRef.child(StudentMatric).child("Attendance").push();
                      StudentMatricRef.child("Status").setValue(AttendanceRecord);
                      StudentMatricRef.child("Date").setValue(getCurrentDate());
                      ProgressUploadAttendance.setProgress(progress);
                  } else {
                      Toast.makeText(TextExtractionActivity.this, "Could not Find " + StudentMatric, Toast.LENGTH_LONG).show();
                  }
    
              }
    
              @Override
              public void onCancelled(DatabaseError databaseError) {
                  throw databaseError.toException(); // don't ignore errors
              }
          });
    
      }