为什么字符串无法转换为对象类

时间:2018-04-06 14:13:51

标签: java android firebase firebase-realtime-database firebaseui

我的代码可能有什么问题?我使用HashMap在数据库中更容易更新,但每当我尝试将数据从数据库提取到FirebaseRecyclerAdapter时,我都会收到错误。 我搜索了许多类似的问题,尝试了他们的解决方案,但没有工作。

以下是例外

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.project.android.designlibdemo.model.RecipeModel

Getter和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类

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



}

我如何将数据发送到数据库

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

                        }

我如何检索数据

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

}

enter image description here

1 个答案:

答案 0 :(得分:1)

要获取帖子接收列表,那么您的数据库应该是:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child
            ("PostRecipe").child(recipeKey);

而是:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child
            ("PostRecipe");

您正在引用特定的PostRecipe,然后当Firebase UI尝试获取对象时,它只找到了字符串名称。

相关问题