如何从Parse中删除特定对象?

时间:2017-11-23 20:00:17

标签: android

我有一个名为Image的解析表。 我想,在点击屏幕上的对象时,我可以在解析中删除它。 此表中的用户标识为imageuserid,该行的标识为objectId。

我的活动代码:

    public class DeletePostsActivity extends AppCompatActivity {

        private String DELETE_LIST;
        private ArrayList<ParseObject> delete;
        private ArrayAdapter<ParseObject> adapter;
        private ParseQuery<ParseObject> query;
        private ListView listView;
        private TextView txtDelete;
        private ProgressDialogUtils progress;
        private View parentLayout;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_delete_posts);
            parentLayout = this.findViewById(android.R.id.content);

            DELETE_LIST = SaveSharedPreferences.getUserId(this);
            progress = new ProgressDialogUtils();

            txtDelete = (TextView)findViewById(R.id.txtDeleteEmpty);
            delete = new ArrayList<>();
            listView = (ListView)findViewById(R.id.listDelete);
            adapter = new DeleteAdapter(this, delete);
            listView.setAdapter( adapter );

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    progress.startProgress(getContext());
                    deleteItem(position);
                }
            });

            Snackbar.make(parentLayout,getString(R.string.item_click_delete),Snackbar.LENGTH_LONG)
                    .setAction("Action",null).show();
        }

        private void deleteItem(int position) {//how to do this???
//How to delete the specific item?
        }

        @Override
        public void onStart() {
            super.onStart();
            getPostsToDelete();
        }

        //catch the posts from user to show
        private void getPostsToDelete() {
            progress.startProgress(this);

            query = ParseQuery.getQuery("Imagem");
            query.whereEqualTo("imageuserid", DELETE_LIST);
            query.orderByDescending("createdAt");

            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> objects, ParseException e) {
                    progress.endProgress(getContext());
                    if (e == null) {//sucesso

                        if (objects.size() > 0) {
                            if (txtDelete.getVisibility() == View.VISIBLE) {//retira o texto da tela se houver catálogo
                                txtDelete.setVisibility(View.GONE);
                            }
                            delete.clear();
                            for (ParseObject parseObject : objects) {
                                delete.add(parseObject);
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            txtDelete.setVisibility(View.VISIBLE);//mostra um texto somente para que a tela não fique em branco
                        }

                    } else {//erro
                        e.printStackTrace();
                    }

                }
            });
        }

        private Context getContext() {
            return this;
        }
    }

我在这个问题上需要帮助,我无法删除此特定对象。 如何删除特定行?

1 个答案:

答案 0 :(得分:0)

这解决了我的问题:

deleteItem方法:

private void deleteItem(final String objectId, final int position) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle(getString(R.string.delete_post_title))
            .setMessage(getString(R.string.delete_post_message))
            .setPositiveButton(getString(R.string.positive_button), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    progress.startProgress(getContext());
                    query = ParseQuery.getQuery("Imagem");
                    query.whereEqualTo("objectId", objectId);
                    query.findInBackground(new FindCallback<ParseObject>() {
                        @Override
                        public void done(List<ParseObject> objects, ParseException e) {
                            if (e == null) {
                                if (objects.size() > 0) {
                                    objects.get(position).deleteInBackground(new DeleteCallback() {
                                        @Override
                                        public void done(ParseException e) {
                                            if (e == null) {
                                                progress.endProgress(getContext());
                                                adapter.notifyDataSetChanged();
                                                Toast.makeText(getContext(),getString(R.string.deleted_item),Toast.LENGTH_SHORT).show();
                                                Snackbar.make(parentLayout, getString(R.string.deleted_item), Snackbar.LENGTH_LONG)
                                                        .setAction("Action", null).show();
                                                adapter.clear();
                                                getPostsToDelete();
                                            } else {
                                                Snackbar.make(parentLayout, getString(R.string.error_to_delete), Snackbar.LENGTH_LONG)
                                                        .setAction("Action", null).show();
                                                progress.endProgress(getContext());
                                            }
                                        }
                                    });
                                }
                            } else {
                                Snackbar.make(parentLayout, getString(R.string.error_to_delete), Snackbar.LENGTH_LONG)
                                        .setAction("Action", null).show();
                                progress.endProgress(getContext());
                            }
                        }
                    });
                }
            })
            .setNegativeButton(getString(R.string.negative_button), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).show();
}