从ParseQueryAdapter中删除项目

时间:2014-09-25 19:49:11

标签: android listview parse-platform

我对Parse和Android开发有点新,所以如果这个问题是陈腐或烦人,请原谅。我已经在Parse论坛上查看了从ParseQueryAdapter中删除项目的好方法,但没有找到任何令人满意的结果。

我想要的是当用户确认要删除/添加的项目时,该项目立即被删除/添加到ListView并在后台更新Parse服务器。帮助

final ParseUser currentUser = ParseUser.getCurrentUser();

    final ParseQueryAdapter<ParseObject> mainAdapter =
            new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
                public ParseQuery<ParseObject> create() {
                    // Here we can configure a ParseQuery to our heart's desire.
                    ParseQuery query = new ParseQuery("todo");
                    //query.whereEqualTo("user", currentUser);
                    return query;
                }
            });
    mainAdapter.setTextKey("title");

    // Set the ListActivity's adapter to be the PQA

    final ListView list = getListView();
    list.setAdapter(mainAdapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            final int pos = position;

            new AlertDialog.Builder(thisactivity)
                    .setTitle("Confirmation Dialog")
                    .setMessage("Do you really want to delete it?")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            ParseObject toBeDeleted = mainAdapter.getItem(pos);
                            toBeDeleted.deleteInBackground();
                            mainAdapter.loadObjects();
                        }})
                    .setNegativeButton(android.R.string.no, null).show();
        }

    });

    final EditText todoName = (EditText)findViewById(R.id.todoName);

    Button adder= (Button) findViewById(R.id.addTodo);
    adder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addTodo(todoName.getText().toString(), currentUser);
            todoName.setText("");
            InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(todoName.getWindowToken(), 0);
            mainAdapter.loadObjects();
        }
    });

1 个答案:

答案 0 :(得分:0)

也许我可以帮你解决这个问题。 以下删除程序:

private void delItem(Basket object) {
    final ParseItems finalitem = object.getParseItem();
    ParseQuery<Basket> query = Basket.getQuery();
    query.setMaxCacheAge(TimeUnit.SECONDS.toMillis(1));

    query.setCachePolicy(ParseQuery.CachePolicy.NETWORK_ELSE_CACHE);
    //query.fromLocalDatastore();
    query.whereEqualTo("parseItem", finalitem);
    query.whereEqualTo("user", ParseUser.getCurrentUser());
    query.whereNotEqualTo("sent", true);
    query.getFirstInBackground(new GetCallback<Basket>() {
        @Override
        public void done(final Basket basket, ParseException e) {
            if (basket != null) {
                basket.increment("quantity", -1);
                basket.saveEventually(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        SuperToast superToast = new SuperToast(context);
                        superToast.setDuration(SuperToast.Duration.VERY_SHORT);
                        superToast.setText("del: " + finalitem.getName() + "-1=" + basket.getQuantity());
                        superToast.setIcon(R.drawable.del, SuperToast.IconPosition.LEFT);
                        superToast.show();
                    }
                });
                if (basket.getQuantity() <= 0) {
                    basket.deleteEventually(new DeleteCallback() {
                        @Override
                        public void done(ParseException e) {
                            SuperToast superToast = new SuperToast(context);
                            superToast.setDuration(SuperToast.Duration.VERY_SHORT);
                            superToast.setText("remove: " + finalitem.getName());
                            superToast.setIcon(R.drawable.del, SuperToast.IconPosition.LEFT);
                            superToast.show();
                        }
                    });
                }
            }
        }
    });