2015-08-25 54 views
1

我希望用戶能夠從列表中刪除已添加好友。我設法修改了一些代碼,但它刪除了Parse「Friend」類中的第一項。我如何修改它以刪除所選用戶?Android如何從解析列表中刪除所選項目

@Override 
    public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, long id) { 
     new MaterialDialog.Builder(MainActivity.this) 
       .title("Remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + "?") 
       .content("Do you really want to remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + " " + "from your friends list?") 
       .positiveText(R.string.dialog_yes) 
       .negativeText(R.string.dialog_no) 
       .callback(new MaterialDialog.ButtonCallback() { 
        @Override 
        public void onPositive(MaterialDialog dialog) { 
         ParseQuery<ParseObject> query = ParseQuery.getQuery("Friend"); 
         query.whereEqualTo("user", mCurrentUser); 
         query.findInBackground(new FindCallback<ParseObject>() { 
          @Override 
          public void done(final List<ParseObject> user, ParseException e) { 
           user.get(position).deleteInBackground(new DeleteCallback() { 
            @Override 
            public void done(ParseException e) { 
             if (e == null) { 
              Toast.makeText(getBaseContext(), "You removed" + " " + mUserFriends.get(position).getUsername().toUpperCase(), Toast.LENGTH_LONG).show(); 
              mUserFriends.remove(position); 
              mFriendAdapter.notifyDataSetChanged(); 
             } else { 
              Toast.makeText(getBaseContext(), "Couldn't remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + ", try again." + e.toString(), Toast.LENGTH_LONG).show(); 
             } 

            } 
           }); 
          } 
         }); 
        } 

        @Override 
        public void onNegative(MaterialDialog dialog) { 
        } 
       }) 
       .show(); 
     return true; 
    } 

回答

1

無論如何,我想到了這一切。以下是代碼, 感謝@Ibukun的提示。

@Override 
     public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, long id) { 
      new MaterialDialog.Builder(MainActivity.this) 
        .title("Remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + "?") 
        .content("Do you really want to remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + " " + "from your friends list?") 
        .positiveText(R.string.dialog_yes) 
        .negativeText(R.string.dialog_no) 
        .callback(new MaterialDialog.ButtonCallback() { 
         // FIXME: 8/25/2015 
         @Override 
         public void onPositive(MaterialDialog dialog) { 
          ParseQuery<ParseObject> query = ParseQuery.getQuery("Friend"); 
          query.whereEqualTo("user", mCurrentUser); 
          query.findInBackground(new FindCallback<ParseObject>() { 
          public void done(List<ParseObject> users, ParseException e) { 
           if (e == null) { 
            // iterate over all users and delete them 
            for (ParseObject user : users) { 
             user.deleteInBackground(); 
              Toast.makeText(getBaseContext(), "You removed" + " " + mUserFriends.get(position).getUsername().toUpperCase(), Toast.LENGTH_LONG).show(); 
              mUserFriends.remove(position); 
              mFriendAdapter.notifyDataSetChanged(); 
             } 
            } else { 
             //Handle condition here 
             Toast.makeText(getBaseContext(), "Couldn't remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + ", try again." + e.toString(), Toast.LENGTH_LONG).show(); 
            } 
           } 
          }); 
         } 

         @Override 
         public void onNegative(MaterialDialog dialog) { 
         } 
        }) 
        .show(); 
      return true; 
     } 
+0

你應該至少投票答案 –

0

它可能是由onItemLongClick函數傳遞的位置參數的問題。大多數時候,函數傳遞的位置只是UI列表的位置,而不是內容的位置。

mUserFriends.remove(position); 

user.get(position).deleteInBackground 

嘗試獲得該元素的位置,並嘗試這樣的事情

mUserFriends.remove(3); // for the remove line 

user.get(position).deleteInBackground // for the get line in the function 

那麼你可以使用這些源作爲參數而不是on e from onItemLongClick