java - 工作条件根据男性和女性的选择而匹配

标签 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");

预先感谢您的支持。

最佳答案

这里有两个步骤:

  • 获取有关当前用户的信息(“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 转换为某种类型的列表才能使上述功能正常工作。

关于java - 工作条件根据男性和女性的选择而匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25249164/

相关文章:

java - Ant 1.9.4 不会使用 Java 8 清理和构建 Netbeans 8.0.2 项目;给出空指针异常

android - MonoGame 3.4 + Android + SoundEffect =没有声音

android - Android 键盘关闭时闪烁?

java - ScrollView 被切入主布局内

java - Spring JMS 并发和 JMSXGroupID

java - 获取所有本地 IP (arp -a)

java - 这个叫什么?这是设计模式还是约定? (接口(interface)/类)

android - 如何以编程方式清除 Android 中的蓝牙名称缓存?

java - Firestore 添加具有引用属性的自定义对象

安卓 ADT 21.0.0。图形布局中的内存泄漏