如何使用ItemLongClick删除Parse中列表中的对象?

时间:2015-10-11 01:13:38

标签: android android-listview parse-platform

我正在努力使其成为如此ItemLong点击评论列表中的评论删除用户的评论,但它不起作用:

private ParseQueryAdapter<Comment> mainCommentAdapter;
private CommentAdapter favoriteCommentAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.post_thread_layout);

mainCommentAdapter = new ParseQueryAdapter<Comment>(this, Comment.class);
mainCommentAdapter.setTextKey("commentText");

favoriteCommentAdapter = new CommentAdapter(this);

final ListView commentsListView = (ListView) findViewById(android.R.id.list);
    commentsListView.setAdapter(mainCommentAdapter);
    commentsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long arg3) {
            final Comment comment = mainCommentAdapter.getItem(position);

            Toast.makeText(getApplicationContext(), commentsListView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();

            AlertDialog.Builder builder = new AlertDialog.Builder(PostThreadActivity.this);
            builder.setTitle("DELETE COMMENT");
            builder.setMessage("Are you sure you want to delete this comment?");
            builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Delete the meal
                    comment.deleteInBackground();
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing
                    dialog.dismiss();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();

            return false;
        }
    });

根据Parse.com文档删除对象简单:

myObject.deleteInBackground();

我做错了什么?

编辑:这是我的Comment.java

import com.parse.ParseClassName;
import com.parse.ParseObject;
import com.parse.ParseUser;

@ParseClassName("Comment")
public class Comment extends ParseObject {
    public Comment() {
        // A default constructor is required.
    }

    public ParseUser getUser() {
        return getParseUser("user");
    }

    public void setUser(ParseUser value) {
        put("user", value);
    }

    public String getCommentText() {
        return getString("commentText");
    }

    public void setCommentText(String commentText) {
        put("commentText", commentText);
    }

    // Associate each comment with a post (meal)
    public void setMeal(Meal meal) {
        put("meal", meal);
    }

    // Get the post for this item
    public Meal getMeal()  {
        return (Meal) getParseObject("meal");
    }

}

CommentAdapter.java

public class CommentAdapter extends ParseQueryAdapter<Comment> {

public CommentAdapter(Context context) {
    super(context, new ParseQueryAdapter.QueryFactory<Comment>() {
        public ParseQuery<Comment> create() {
            ParseQuery query = new ParseQuery("Comment");
            query.include("user");
            //query.include("owner"); //<-- from CodePath tutorial delete if not needed
            query.addAscendingOrder("createdAt"); //add setTime(like anywall) to the comments
            return query;
        }

    });

}

@Override
public View getItemView(Comment comment, View v, ViewGroup parent) {

    if (v == null) {
        v = View.inflate(getContext(), R.layout.comment_item, null);
    }

    //show the submitted words in Comments adapter
    TextView commentTextView = (TextView) v.findViewById(R.id.comment_view);  //Get the textView for the title from item_list_favorites.xml and convert it to a variable titleTextView. Then gets the submitted Event title info from getters and setters: getTitle, then set it as text of TitleTextView //simple.
    commentTextView.setText(comment.getCommentText());

    //show username for each comment
    TextView usernameTextView = (TextView) v.findViewById(R.id.username_view);  //Get the textView for the title from item_list_favorites.xml and convert it to a variable titleTextView. Then gets the submitted Event title info from getters and setters: getTitle, then set it as text of TitleTextView //simple.
    usernameTextView.setText(comment.getUser().getUsername());

    return v;
}

}

1 个答案:

答案 0 :(得分:0)

尝试使用回调

comment.deleteInBackground(new DeleteCallback() {

        @Override
        public void done(ParseException e) {
            // TODO Auto-generated method stub
            Log.e("Delete Error", e.getMessage());
        }
    });