DatabaseException:无法转换类型为java.lang.String的对象

时间:2019-05-18 07:03:56

标签: java android firebase-realtime-database casting

我正在使用var Query2 = from b in Books orderby b.BookPubid group b.BookPubid by b.BookPubid into g select new { PublisherID = g.Key, TotalBooks = g.Count() }; recyclerView来检索图像,但不幸的是我遇到了此Firebase错误

cardView

这是我的模型课:

    Process: com.edu.hilmi.demoopencv, PID: 6939
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.edu.hilmi.demoopencv.Sample
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.1.0:423)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:214)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.1.0:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.1.0:212)
        at com.edu.hilmi.demoopencv.SampleList$1.onDataChange(SampleList.java:55)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:150)
        at android.app.ActivityThread.main(ActivityThread.java:5408)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

这是上传活动

public class Sample
{

    private String SampleID;
    private String SampleDate;
    private String SampleTime;
    private String SampleLocation;
    private String SampleImageUrl;

    public Sample(){}

    public Sample(String sampleID, String sampleDate, String sampleTime, String sampleLocation, String sampleImageUrl) {
        SampleID = sampleID;
        SampleDate = sampleDate;
        SampleTime = sampleTime;
        SampleLocation = sampleLocation;
        SampleImageUrl = sampleImageUrl;
    }

    public String getSampleID()
    {
        return SampleID;
    }

    public String getSampleImageUrl()
    {
        return SampleImageUrl;
    }

    public String getSampleDate()
    {
        return SampleDate;
    }

    public String getSampleTime()
    {
        return SampleTime;
    }

    public String getSampleLocation()
    {
        return SampleLocation;
    }

    public void setSampleID(String sampleID) {
        SampleID = sampleID;
    }

    public void setSampleDate(String sampleDate) {
        SampleDate = sampleDate;
    }

    public void setSampleTime(String sampleTime) {
        SampleTime = sampleTime;
    }

    public void setSampleLocation(String sampleLocation) {
        SampleLocation = sampleLocation;
    }

    public void setSampleImageUrl(String sampleImageUrl) {
        SampleImageUrl = sampleImageUrl;
    }
}

这是检索图像和其他数据的活动

  public void addSample() {
   if (FilePathUri != null) {
    progressDialog.setTitle("Image is Uploading...");
    progressDialog.show();
    final StorageReference storageReference2nd = storageReference.child(Storage_Path + System.currentTimeMillis() + "." + GetFileExtension(FilePathUri));
    storageReference2nd.putFile(FilePathUri)
     .addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
      @Override
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
       Uri downloadUrl = FilePathUri;
       String usableUrl = downloadUrl.toString();
       String sampleID = SamID.getText().toString().trim();
       String sampleDate = DateText.getText().toString().trim();
       String sampleTime = TimeText.getText().toString().trim();
       String sampleLocation = LocText.getText().toString().trim();

       progressDialog.dismiss();

       Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();

       @SuppressWarnings("VisibleForTests")
       Sample sample = new Sample(sampleID, sampleDate, sampleTime, sampleLocation, usableUrl);
       databaseReference.child(sampleID).setValue(sample);
       SamID.setText("");
       DateText.setText("");
       TimeText.setText("");
       LocText.setText("");

       Intent intent = new Intent(RegisterSample.this, SampleList.class);
       startActivity(intent);
      }

     })

     .addOnFailureListener(new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception exception) {
       progressDialog.dismiss();
       Toast.makeText(RegisterSample.this, exception.getMessage(), Toast.LENGTH_LONG).show();
      }
     })

     .addOnProgressListener(new OnProgressListener < UploadTask.TaskSnapshot > () {
      @Override
      public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
       // Setting progressDialog Title.
       progressDialog.setTitle("Image is Uploading...");

      }
     });

这是适配器类

public class SampleList extends AppCompatActivity
{
    DatabaseReference mRef;
    StorageReference storageRef;
    RecyclerView recyclerView;
    ArrayList<Sample> list;
    SampleListAdapter sampleListAdapter;

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

        if (getSupportActionBar() != null)
            getSupportActionBar().setTitle("Sample List");

        recyclerView = findViewById(R.id.ImgViewer);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);

        //storageRef = FirebaseStorage.getInstance().getReference().child("Sample_images/");
        mRef = FirebaseDatabase.getInstance().getReference().child("Samples");

        mRef.addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                list = new ArrayList<Sample>();

                for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                {
                    Sample s= dataSnapshot1.getValue(Sample.class);
                    list.add(s);
                }
                sampleListAdapter = new SampleListAdapter(SampleList.this,list);
                recyclerView.setAdapter(sampleListAdapter);
                sampleListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError)
            {
                Toast.makeText(SampleList.this, "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

图片应显示在public class SampleListAdapter extends RecyclerView.Adapter < SampleListAdapter.ImageViewHolder > { private Context mContext; private List < Sample > mSample; public SampleListAdapter(Context context, List < Sample > samples) { mContext = context; mSample = samples; } @NonNull @Override public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(mContext).inflate(R.layout.activity_sample_list_item, viewGroup, false); return new ImageViewHolder(v); } @Override public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int position) { Sample sample = mSample.get(position); imageViewHolder.textViewID.setText(sample.getSampleID()); Picasso.get() .load(sample.getSampleImageUrl()) .placeholder(R.drawable.photo) .fit() .centerInside() .into(imageViewHolder.imageView); } @Override public int getItemCount() { return mSample.size(); } public class ImageViewHolder extends RecyclerView.ViewHolder { TextView textViewID; ImageView imageView; ImageViewHolder(View itemView) { super(itemView); textViewID = itemView.findViewById(R.id.text_view_id); imageView = itemView.findViewById(R.id.image_view_upload); } } } cardView

0 个答案:

没有答案