Reputation: 967
What could be wrong in my code? I use HashMap
to make update easier in the database, but whenever I try to fetch this data from the database to FirebaseRecyclerAdapter
I keep getting an error.
I have searched for many similar problems, tried their solutions but not working.
Below is the exception
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.project.android.designlibdemo.model.RecipeModel
Getter and setter
public class RecipeModel {
private String name;
public RecipeModel(){
}
public RecipeModel(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
ViewHolder class
public class ViewHolderRecipe extends RecyclerView.ViewHolder {
String LOG_TAG = ViewHolderRecipe.class.getSimpleName();
public View view;
private TextView ingredient_txt;
public ViewHolderRecipe(View itemView) {
super(itemView);
view = itemView;
ingredient_txt = itemView.findViewById(R.id.ingredient_list);
}
public void setRecipeName(String recipe){
ingredient_txt.setText(recipe);
}
How i send data to database
Map postRecipeMap = new HashMap();
final DatabaseReference sendRecp = FirebaseDatabase.getInstance().getReference().child
("Post").child("PostRecipe").push();
sendRecp.updateChildren(postRecipeMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mProgressDialog.dismiss();
AlertDialog.Builder alertDialogBuilder = new android.support.v7.app
.AlertDialog.Builder(PostingRecipeActivity.this);
alertDialogBuilder.setTitle("Successfully");
alertDialogBuilder.setMessage("You can click next");
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//Launch PostDetailActivity
Intent i = new Intent(PostingRecipeActivity.this, ContentIngredientList.class);
final String reportKey = sendRecp.getKey();
i.putExtra(ContentIngredientList.EXTRA_FIR_KEY, reportKey);
mProgressDialog.dismiss();
startActivity(i);
}
});
AlertDialog aDialog = alertDialogBuilder.create();
aDialog.show();
}
How I retrieve the data
public class ContentIngredientList extends AppCompatActivity {
public static final String EXTRA_FIR_KEY = "recipeKey";
private FirebaseRecyclerAdapter<RecipeModel, ViewHolderRecipe> mAdapter;
//private RecyclerView mIngredient_list;
String LOG_TAG = PostingRecipeActivity.class.getSimpleName();
private String recipeKey;
public ContentIngredientList(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_ingredient_list);
// Get post key from intent
recipeKey = getIntent().getStringExtra(EXTRA_FIR_KEY);
if (recipeKey == null) {
throw new IllegalArgumentException("Must pass EXTRA_POST_KEY");
}
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child
("PostRecipe").child(recipeKey);
RecyclerView mRcyclerViewIngredient = findViewById(R.id.ingredient_list_recycler);
//mIngredient_list = findViewById(R.id.ingredient_list);
Log.e(LOG_TAG, "url" + mDatabase.getRef()) ;
// [END create_database_reference]
// mDirectionList.setHasFixedSize(true);
mRcyclerViewIngredient.setLayoutManager(new LinearLayoutManager(this));
// mIngredient_list.setHasFixedSize(true);
//mIngredient_list.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new FirebaseRecyclerAdapter<RecipeModel, ViewHolderRecipe>(RecipeModel.class,
R.layout.ingredient_posting, ViewHolderRecipe.class, mDatabase) {
@Override
protected void populateViewHolder(ViewHolderRecipe viewHolder, RecipeModel model, int position) {
viewHolder.setRecipeName(model.getName());
}
};
mRcyclerViewIngredient.setAdapter(mAdapter);
}
}
Upvotes: 0
Views: 313
Reputation: 12005
To get the list of post recepies, then your database shoundn't be:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child
("PostRecipe").child(recipeKey);
But rather:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child
("PostRecipe");
You were referencing a particular PostRecipe, then when Firebase UI tried to get the object it only found the string name.
Upvotes: 1