Back4App,如何更新关系数据数组

问题描述

每当我尝试将 Reviews 类数据保存到我的 Post 类时,终端都会输出此消息,但我不确定如何正确更新 back4app 中的关系数据 这是错误消息。 com.parse.ParseException:java.lang.IllegalStateException:无法编码与未保存的 ParSEObject 的关联

package com.example.rentahome.fragments;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.linearlayoutmanager;

import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ratingBar;
import android.widget.Toast;

import com.example.rentahome.Post;
import com.example.rentahome.R;
import com.example.rentahome.ReviewAdapter;
import com.example.rentahome.Reviews;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParSEObject;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ReviewFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ReviewFragment extends Fragment {

    // Todo: Rename parameter arguments,choose names that match
    // the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
    private static final String ARG_ParaM1 = "param1";
    private static final String ARG_ParaM2 = "param2";

    public Post loadpost;
    public EditText description;
    public ratingBar ratingBar;
    public Button submit;

    public void updatePost(Post post){
        loadpost = post;
    }

    // Todo: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public ReviewFragment() {
        // required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ReviewFragment.
     */
    // Todo: Rename and change types and number of parameters
    public static ReviewFragment newInstance(String param1,String param2) {
        ReviewFragment fragment = new ReviewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_ParaM1,param1);
        args.putString(ARG_ParaM2,param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_ParaM1);
            mParam2 = getArguments().getString(ARG_ParaM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_review,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState){
        super.onViewCreated(view,savedInstanceState);




        description = view.findViewById(R.id.submitReview_description);
        ratingBar = view.findViewById(R.id.submitReview_ratingbar);
        submit = view.findViewById(R.id.submitReview_submitbtn);

        submit.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ParseRelation<ParSEObject> relation = loadpost.getRelation("Reviews");

                if(ratingBar.getNumStars() == 0){
                    Toast.makeText(getContext(),"Rate the house!",Toast.LENGTH_SHORT).show();
                }else if(description.getText().toString().isEmpty()){
                    Toast.makeText(getContext(),"Describe the house!",Toast.LENGTH_SHORT).show();
                }else{
                    ParSEObject temp = ParSEObject.create("Reviews");
                    temp.put("Description",description.getText().toString());
                    temp.put("author",ParseUser.getCurrentUser());
                    temp.put("likesCount",0);
                    temp.put("dislikesCount",0);
                    temp.put("rating",(float)ratingBar.getNumStars());


                    temp.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {

                        }
                    });


                    relation.add(temp);


                    loadpost.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            if(e!=null){
                                Log.e("ReviewFragment","Issue with saving posts..",e);
                                Toast.makeText(getContext(),"Error while saving",Toast.LENGTH_SHORT).show();
                                return;
                            }

                        }
                    });

                    Homefragment nextFrag= new Homefragment();

                    getActivity().getSupportFragmentManager().beginTransaction()
                            .replace(R.id.flContainer,nextFrag)
                            .addToBackStack(null)
                            .commit();
                }

            }
        });

    }
}

基本的想法是从现有的帖子数据中获取一个加载帖子并创建一个评论对象,然后将它附加到加载帖子并保存它。 我使用此文档来尝试找出我的解决方https://docs.parseplatform.org/android/guide/#relational-data

解决方法

Reviews temp2 = new Reviews();
                    temp2.setAuthor(ParseUser.getCurrentUser());
                    temp2.setDescription(description.getText().toString());
                    temp2.setRating((float)ratingBar.getNumStars());
                    temp2.setdislikesCount(0);
                    temp2.setlikesCount(0);
                    temp2.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            if(e!=null){
                                Log.e("ReviewFragment","Issue with saving posts..",e);
                                Toast.makeText(getContext(),"Error while saving",Toast.LENGTH_SHORT).show();
                                return;
                            }

                            relation.add(temp2);
                            loadpost.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(ParseException e) {
                                    if(e!=null){
                                        Log.e("ReviewFragment",e);
                                        Toast.makeText(getContext(),Toast.LENGTH_SHORT).show();
                                        return;
                                    }

                                }
                            });

                        }
                    });



我所要做的就是调用一个对象并将其保存在后台保存函数中。