我正在尝试将已添加到我的Firebase数据库的数据显示到android。数据很好地上传到数据库,但当我尝试通过手机或模拟器显示时,我收到以下错误:
com.google.firebase.database.DatabaseException:无法转换 java.util.HashMap类型的值为String
以下内容:
com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
这是第146和147行的代码:
protected T parseSnapshot(DataSnapshot snapshot) {
return snapshot.getValue(mModelClass);
}
此错误导致应用程序崩溃而不运行。我相信错误是由于我的MainActivity类中的这段代码,因为当我删除应用程序将运行的.child元素时,但显然不会显示数据。
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
我不确定为什么会抛出此错误,因为我在数据库中使用的唯一数据类型是字符串,并且子“Blog”在我的数据库中拼写正确。
另一个重要信息: 当我删除firebase中的“Blog”子项时,该应用程序将运行。但是,一旦我向firebase提交数据,就会发生错误并且我的应用程序崩溃了。
任何帮助将不胜感激,
此类显示数据:
public class MainActivity extends AppCompatActivity {
private RecyclerView mPostList;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mPostList = (RecyclerView) findViewById(R.id.post_list);
mPostList.setHasFixedSize(true);
mPostList.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.post_row,
BlogViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView post_title = (TextView)mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc) {
TextView post_desc = (TextView)mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image) {
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== R.id.action_add){
startActivity(new Intent(MainActivity.this, PostActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
此类将数据发布到firebase:
package infectionapp.com.infectionui;
public class PostActivity extends AppCompatActivity {
private ImageButton mSelectImage;
private Uri mImageUri = null;
private EditText mPostTitle;
private EditText mPostDesc;
private Button mSubmitBtn;
private StorageReference mStorage;
private DatabaseReference mDatabase;
// private ProgressBar mProgress;
private static final int GALLERY_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
//Directs to root firebase storage
mSelectImage = (ImageButton) findViewById(R.id.imageSelect);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mPostTitle = (EditText) findViewById(R.id.titleField);
mPostDesc = (EditText)findViewById(R.id.descField);
mSubmitBtn = (Button) findViewById(R.id.submitBtn);
// mProgress = new ProgressBar(this);
mSubmitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startPosting();
}
});
mSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
});
}
private void startPosting() {
final String title_val = mPostTitle.getText().toString().trim();
final String desc_val = mPostDesc.getText().toString().trim();
if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null){
StorageReference filepath = mStorage.child("Post_Images").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
//Creates unique ID for post
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(desc_val);
newPost.child("image").setValue(downloadUrl).toString();
startActivity(new Intent(PostActivity.this, MainActivity.class));
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
mImageUri = data.getData();
mSelectImage.setImageURI(mImageUri);
}
}
}
答案 0 :(得分:1)
为了以最简单的方式获取数据,我使用此代码重新命令:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference blogRef = rootRef.child("Blog");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String desc = ds.child("desc").getValue(String.class);
String title = ds.child("title").getValue(String.class);
String image = ds.child("image").child("path").getValue(String.class);
Log.d("TAG", desc + " / " + title + " / " + image);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
blogRef.addListenerForSingleValueEvent(eventListener);
您的输出将是:
tst / test / /v0/b/infectionui...
如您所见,我使用过String类。这也是您在FirebaseRecyclerAdapter
。
如果您想从图片中获取其他价值,只需将.child("path")
改为另一个孩子,让我们说.child("schemeSpecificPart")
。