代码之家  ›  专栏  ›  技术社区  ›  olajide Marcos Vasconcelos

为什么字符串不能转换为对象类

  •  0
  • olajide Marcos Vasconcelos  · 技术社区  · 6 年前

    我的代码中可能有什么错误?我使用 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 回复  |  直到 6 年前
        1
  •  1
  •   Levi Moreira    6 年前

    要获取post Receipes列表,则您的数据库不应为:

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

    而是:

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

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