如何在Parse对象中删除而不是重复?

时间:2016-01-28 18:05:44

标签: java android parse-platform

我正在尝试从Parse云中删除一个Row(一个Object)。而不是删除它复制对象。我试图从我的解析云/数据库中删除selectedPost。我将在AlertDialog中删除它:

builder.setNeutralButton("DELETE POST", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            try {
                selectedPost.delete();
            } catch (ParseException e) {
                e.printStackTrace();
                Log.e("post", "error " + e);
            }
            selectedPost.saveInBackground();
            dialog.dismiss();
        }
    });

MyPostsFragment:

public class MyPostsFragment extends Fragment {

ListView lv;
ArrayAdapter adapter;
ArrayList<Post> postArrayList;
Post selectedPost;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_myposts, container, false);

    lv = (ListView) rootView.findViewById(R.id.mypost_listview);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // clicked on item show post
            selectedPost = postArrayList.get(position);
            Bundle bundle = new Bundle();
            bundle.putParcelable("data", (Parcelable) selectedPost);
            FragmentManager fm = getActivity().getFragmentManager();
            Fragment fragment = new rang.afterflight.fragments.SelectedPostFragment();
            fragment.setArguments(bundle);
            fm.beginTransaction().replace(R.id.content_main, fragment).commit();
        }

    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            selectedPost = postArrayList.get(position);
            showDialog();
            return true;
        }
    });

    return rootView;
}




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ownPost();

    setHasOptionsMenu(true);


}

public void ownPost(){
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");

    ParseUser currentUser = ParseUser.getCurrentUser();
    String user = currentUser.getUsername();

    postArrayList = new ArrayList<Post>();

    query.whereEqualTo("username", user);

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> postList, ParseException e) {
            if (e == null) {
                for (ParseObject object : postList) {
                    Post newPost = new Post();
                    newPost.setAirportParse((String) object.get("airport"));
                    newPost.setDateParse((String) object.get("date"));
                    newPost.setTimeParse((String) object.get("time"));
                    newPost.setPersonsParse((String) object.get("persons"));
                    newPost.setAddressParse((String) object.get("address"));
                    newPost.setFlightnrParse((String) object.get("flightnr"));
                    newPost.setUsername((String) object.get("username"));
                    newPost.setImageFile((ParseFile) object.get("profilepic"));
                    newPost.setContactParse((String) object.get("contact"));
                    newPost.setId(object.getObjectId());

                    postArrayList.add(newPost);
                }

                adapter = new ListViewAdapter(getActivity(), R.layout.item_cardview, postArrayList);
                lv.setAdapter(adapter);
            }
        }
    });
}

private void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater factory = LayoutInflater.from(getActivity());
    final View view = factory.inflate(R.layout.dialog_deletepost, null);

    builder.setView(view);
    builder.setCancelable(false);
    builder.setTitle("Are you sure you want to delete?");
    builder.setPositiveButton("GO BACK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });

    builder.setNeutralButton("DELETE POST", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");

            query.whereEqualTo("objectId", selectedPost.getObjectId());

            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> postList, ParseException e) {
                    if (e == null) {
                        for (ParseObject post : postList) {
                            post.deleteInBackground();
                        }
                    }
                }
            });
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
}

发表:

@ParseClassName("Post")

public class Post extends ParseObject实现Serializable,Parcelable {

public Post(){
    super();
}

public String getId(){
    return getString("objectId");
}

public void setId(String id){
    put("objectId", id);
}

//////////

public String getUsername(){
    return getString("username");
}

public void setUsername(String username){
    put("username", username);
}



public String getAirportParse(){
    return getString("airport");
}

public void setAirportParse(String airport){
    put("airport", airport);
}

//////////


public String getDateParse(){
    return getString("date");
}

public void setDateParse(String date){
    put("date", date);
}


//////////


public String getTimeParse(){
    return getString("time");
}

public void setTimeParse(String time){
    put("time", time);
}


//////////


public String getPersonsParse(){
    return getString("persons");
}

public void setPersonsParse(String persons){
    put("persons", persons);
}


//////////


public String getAddressParse(){
    return getString("address");
}

public void setAddressParse(String address){
    put("address", address);
}

public String getFlightnrParse(){
    return getString("flightnr");
}

public void setFlightnrParse(String flightnr){
    put("flightnr", flightnr);
}

public String getContactParse(){
    return getString("contact");
}

public void setContactParse(String contact){
    if(contact != null){
        put("contact", contact);
    }
}


public Bitmap getImageFile(){
    Bitmap bmp = null;
    ParseFile image = getParseFile("profilepic");
    if(image != null){
        try {
            byte[] data = image.getData();
            bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
        } catch (ParseException e) {
            e.printStackTrace();
            }
    }
    return bmp;
}

public void setImageFile(ParseFile file) {
    if (file != null) {
        put("profilepic", file);
    }
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
}

protected Post(Parcel in) {
}

public static final Parcelable.Creator<Post> CREATOR = new Parcelable.Creator<Post>() {
    public Post createFromParcel(Parcel source) {
        return new Post(source);
    }

    public Post[] newArray(int size) {
        return new Post[size];
    }
};
}

2 个答案:

答案 0 :(得分:1)

要删除尝试根据条件检索项目并将其删除。我会向Post添加一个ObjectID属性并检索并删除它。

所以在ownPost()执行此操作

newPost.setObjectID(object.getObjectId());

为帖子设置objectID后,您需要在长按后再次在Parse中找到该对象。因此,当您单击“删除”时,您希望获取该对象,然后将其删除。

     builder.setNeutralButton("DELETE POST", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        try {
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");

            query.whereEqualTo("ObjectID", selectedPost.getObjectId());

            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> postList, ParseException e) {
                    if (e == null) {
                        for (ParseObject post : postList) {
                            post.deleteInBackground();
                        }
                    }
                }
            });
        } catch (ParseException e) {
            e.printStackTrace();
            Log.e("post", "error " + e);
        }
        dialog.dismiss();
    }
});

答案 1 :(得分:0)

删除ParseObject后,无需调用saveInBackground()。只需致电deleteInBackground()即可完成任务。