从firebase中的MainActivity上传RecyclerView的所有数据

问题描述 投票:0回答:1

我正在处理一些图像并将数据列表中的过程数据添加到RecyclerView。现在我想通过检查List中的对象是否与firebase数据库子项匹配,然后将第二个对象逐个插入到匹配的firebase子项的子项中,将所有数据插入到firebase数据库中。如何通过主要活动中的单个按钮而不是RecyclerView来实现。

这是我如何使用ModelClass在RecyclerView中插入数据。

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;

 } 

想要的是什么

 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);

       }

 });

我现在的问题是如何在btnUploadData.setOnclickliastener中获取数据(Student [i],Attendance [i])并将数组List的第一个元素与firebase键匹配,并在键的子元素中插入下一个元素?我被困在这里好几天了。任何建议都会非常贴切。提前致谢

android firebase android-recyclerview insert-update
1个回答
0
投票

我没有意识到答案很简单..这就是我所做的

 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
          }
      });

  }
© www.soinside.com 2019 - 2024. All rights reserved.