根据他们的选择制定符合男性和女性的条件

时间:2014-08-11 17:36:20

标签: java android android-layout android-activity parse-platform

我正处于这种困境,我试图用解析来找出这个匹配系统。让我们说用户选择男性或女性,然后选择他正在寻找男性或女性。

我无法回答相反的决定,因为如果男性正在寻找男性,或者女性正在寻找女性。

我的问题如下:如何通过解析设置条件,根据用户正在查找的性别返回用户列表。换句话说,我想要回复用户的异性,除非他们正在寻找同性

有了支持,这些是我到目前为止所设定的条件。

ParseQuery<ParseUser> query = ParseUser.getQuery();
           query.whereNotEqualTo("objectId", currentUserId);
               query.whereEqualTo("Gender","female"); 

以下是允许用户将其信息记录到Parse.com的代码

mName = (EditText)findViewById(R.id.etxtname);
        mAge = (EditText)findViewById(R.id.etxtage);
        mHeadline = (EditText)findViewById(R.id.etxtheadline);
        mprofilePicture = (ImageView)findViewById(R.id.profilePicturePreview);
        male = (RadioButton)findViewById(R.id.rimale);
        female = (RadioButton)findViewById(R.id.rifemale);
        lmale = (RadioButton)findViewById(R.id.rlmale);
        lfemale = (RadioButton)findViewById(R.id.rlfemale);

        mConfirm = (Button)findViewById(R.id.btnConfirm);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = mName.getText().toString();
                String age = mAge.getText().toString();
                String headline = mHeadline.getText().toString();



                age = age.trim();
                name = name.trim();
                headline = headline.trim();

                if (age.isEmpty() || name.isEmpty() || headline.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                    builder.setMessage(R.string.signup_error_message)
                        .setTitle(R.string.signup_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else {
                    // create the new user!
                    setProgressBarIndeterminateVisibility(true);

                    ParseUser currentUser = ParseUser.getCurrentUser();


                    if(male.isChecked())
                        gender = "Male";
                    else
                        gender = "Female";

                    if(lmale.isChecked())
                        lgender = "Male";
                    else
                        lgender = "Female";


                    currentUser.put("Name", name); 
                    currentUser.put("Age", age); 
                    currentUser.put("Headline", headline); 
                    currentUser.put("Gender", gender);
                    currentUser.put("Looking_Gender", lgender);

                    currentUser.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                // Success!
                                Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                            else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                                builder.setMessage(e.getMessage())
                                    .setTitle(R.string.signup_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });

更新根据我收到的建议,下面是代码。我不确定这是否具有逻辑意义,因为它现在似乎不起作用。我在

之间添加了我的评论
currentUserId = ParseUser.getCurrentUser().getObjectId();
ParseQuery<ParseUser> query = ParseUser.getQuery();
               //It cannot return the current user for you can't possibly match yourself
               query.whereNotEqualTo("objectId", currentUserId);
               // If current user is a male, is looking for a female, than return female              
             query.whereEqualTo("Gender","Male").whereEqualTo("Looking_Gender","Female");
              // If current user is looking for a female, looking for a male than return male
             query.whereEqualTo("Gender","Female").whereEqualTo("Looking_Gender","Female");

            //if current user is a female, and is looking for a female than return female   
             query.whereEqualTo("Looking_Gender","Female").whereEqualTo("Gender","Female");
            //if current user is a male and is looking for a male, than return a male
             query.whereEqualTo("Looking_Gender","Male").whereEqualTo("Gender","Male");

提前感谢您的支持。

1 个答案:

答案 0 :(得分:1)

这里有两个步骤:

  • 获取有关当前用户的信息(“Gender”和“Looking_Gender”)
  • 查找具有兼容数据的其他用户

除非当前用户在其他地方更新,否则您只需执行以下操作即可获取其详细信息:

String userGender = ParseUser.getCurrentUser().getString("Gender");
String userLookingGender = ParseUser.getCurrentUser().getString("Looking_Gender");

现在,您想要找到性别与当前用户所需内容匹配的其他用户,以及正在寻找当前用户性别的人员:

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
// users with Gender = currentUser.Looking_Gender
query.whereEqualTo("Gender", userLookingGender);
// users with Looking_Gender = currentUser.Gender
query.whereEqualTo("Looking_Gender", userGender);

当然,你目前只处理异性恋和同性恋欲望,双性恋者呢?

要解决此问题,您需要将Looking_Gender列更改为数组并将其命名为Looking_Genders。然后,您可以修改代码来处理它。

更改获取当前用户正在寻找的性别的代码:

JSONArray userLookingGenders = ParseUser.getCurrentUser().getJSONArray("Looking_Genders");

更改查询:

// users with Gender contained in currentUser.Looking_Genders
query.whereContainedIn("Gender", userLookingGenders);
// users with Looking_Genders contains currentUser.Gender
// NOTE: no change other than column name is now plural (Looking_Gender vs Looking_Genders)
query.whereEqualTo("Looking_Genders", userGender);

我从未真正编写任何Android代码,因此您可能需要执行诸如将JSONArray转换为某种类型的列表以实现上述工作。