我在下面形成并执行我的if语句时遇到问题。我的问题如下 - 如何通过解析设置条件,根据用户选择的性别,并根据用户正在查找的性别返回用户列表。换句话说,我想要回复用户的异性,除非他们正在寻找同性。
我尝试使用一系列if语句来实现这一点,但遇到了以下错误等问题:
The operator && is undefined for the argument type(s) ParseQuery<ParseUser>, boolean
以下是if语句代码
if(query.whereEqualTo("Looking_Gender","Male") != null)
query.whereNotEqualTo("Looking_Gender","Male");
if(query.whereEqualTo("Looking_Gender","Female") != null)
query.whereNotEqualTo("Looking_Gender","Female");
if(query.whereEqualTo("Looking_Gender","Female") && (query.whereEqualTo("Gender","Female") != null)
query.whereEqualTo("Looking_Gender","Female");
else
query.whereEqualTo("Looking_Gender","Female");
if(query.whereEqualTo("Looking_Gender","Male") && (query.whereEqualTo("Gender","Male") != null)
query.whereEqualTo("Looking_Gender","Male");
else
query.whereEqualTo("Looking_Gender","Female");
以下是我的整个代码
public class MatchingActivity extends Activity {
private String currentUserId;
private ArrayAdapter<String> namesArrayAdapter;
private ArrayList<String> names;
private ListView usersListView;
private Button logoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matching);
logoutButton = (Button) findViewById(R.id.logoutButton);
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ParseUser.logOut();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
});
setConversationsList();
}
private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
String userActivitySelectionName = null;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo("objectId", currentUserId);
query.whereEqualTo("ActivityName",userActivitySelectionName);
if(query.whereEqualTo("Looking_Gender","Male") != null)
query.whereNotEqualTo("Looking_Gender","Male");
if(query.whereEqualTo("Looking_Gender","Female") != null)
query.whereNotEqualTo("Looking_Gender","Female");
if(query.whereEqualTo("Looking_Gender","Female") && query.whereEqualTo("Gender","Female") != null)
query.whereEqualTo("Looking_Gender","Female");
else
query.whereEqualTo("Looking_Gender","Female");
if(query.whereEqualTo("Looking_Gender","Male") && query.whereEqualTo("Gender","Male") != null)
query.whereEqualTo("Looking_Gender","Male");
else
query.whereEqualTo("Looking_Gender","Female");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> userList, ParseException e) {
if (e == null) {
for (int i=0; i<userList.size(); i++) {
names.add(userList.get(i).getUsername().toString());
}
usersListView = (ListView)findViewById(R.id.usersListView);
namesArrayAdapter =
new ArrayAdapter<String>(getApplicationContext(),
R.layout.user_list_item, names);
usersListView.setAdapter(namesArrayAdapter);
usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
openConversation(names, i);
}
});
} else {
Toast.makeText(getApplicationContext(),
"Error loading user list",
Toast.LENGTH_LONG).show();
}
}
});
}
public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, ParseException e) {
if (e == null) {
Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Error finding that user",
Toast.LENGTH_SHORT).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");
Anyhelp将不胜感激
答案 0 :(得分:3)
这是因为whereEqualTo
返回一个查询,而不是一个布尔值。您应该链接这些查询,而不是将它们用作if
语句。有点像:
query.whereEqualTo("Looking_Gender","Female").whereEqualTo("Gender","Female")
答案 1 :(得分:2)
if (query.whereEqualTo("Looking_Gender","Female") && (query.whereEqualTo("Gender","Female") != null)
这会检查query.whereEqualTo("Looking_Gender","Female")
是第一个。它是什么?它是一个布尔值吗?如果没有,则需要将其与某些东西进行比较以产生布尔值。
我相信它只是ParseQuery<ParseUser>
,尽管你还没告诉我们。
总的来说,您需要这种语法:
if (boolean_expression && boolean_expression)
和&amp;&amp;的左侧在上面的代码行中不是布尔表达式。你有两次错误。
您可能希望将其与null进行比较,==或!=。
答案 2 :(得分:1)
您误解了Parse查询的工作方式。每个whereEqualTo()
函数就像添加AND表达式一样。
您最终会搜索&#34; Gender&#34;等于&#34;男性&#34;并且也等于&#34;女性&#34;,这当然不起作用,因为它不能同时等于两个字符串。与您对#34; Looking_Gender&#34;。
的检查相同根据我对您的其他问题的回答,您需要为当前用户读取值,然后在whereEqualTo()
表达式中使用这些值,例如:
// find users with Gender equal to what the current user is looking for
query.whereEqualTo("Gender", currentUserLookingGender);
// and that are looking for people of the current user's gender
query.whereEqualTo("Looking_Gender", currentUserGender);