我在我的Android应用程序中使用parse.com搜索我的用户表并显示结果。它在多个字段中搜索并返回符合搜索字符串的所有用户。这是我的代码:
String searchParams = searchText.getText().toString().trim();
if (!searchParams.isEmpty()) {
final ArrayList<SearchResults> searchResults = new ArrayList<SearchResults>();
listAdapter = new ListAdapter(activityContext, searchResults);
listView.setAdapter(listAdapter);
ParseQuery<ParseUser> queryName = ParseUser.getQuery();
queryName.whereContains("name", searchParams);
ParseQuery<ParseUser> queryPhone = ParseUser.getQuery();
queryPhone.whereContains("username", searchParams);
ParseQuery<ParseUser> queryEmail = ParseUser.getQuery();
queryEmail.whereContains("email", searchParams);
ParseQuery<ParseUser> querySurname1 = ParseUser.getQuery();
querySurname1.whereContains("surname1", searchParams);
ParseQuery<ParseUser> querySurname2 = ParseUser.getQuery();
querySurname2.whereContains("surname2", searchParams);
List<ParseQuery<ParseUser>> queries = new ArrayList<ParseQuery<ParseUser>>();
queries.add(queryName);
queries.add(queryPhone);
queries.add(queryEmail);
queries.add(querySurname1);
queries.add(querySurname2);
ParseQuery<ParseUser> mainQuery = ParseQuery.or(queries);
mainQuery.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
Log.d("USERS", "Retrieved " + objects.size() + " Users");
if (objects.size() > 0) {
for (ParseObject dealsObject : objects) {
Log.i("NAME", dealsObject.getString("name"));
Log.i("PRIVATE", Integer.toString(dealsObject.getInt("private")));
final SearchResults sr1 = new SearchResults();
sr1.setName(dealsObject.getString("name"));
sr1.setSurname1(dealsObject.getString("surname1"));
sr1.setSurname2(dealsObject.getString("surname2"));
sr1.setUid(dealsObject.getObjectId());
Log.d("UID", dealsObject.getObjectId());
if (dealsObject.getInt("private") == 0) {
sr1.setEmail(dealsObject.getString("email"));
sr1.setPhone(dealsObject.getString("username"));
} else {
sr1.setPhone("xxx-xxx-xxx");
sr1.setEmail("xxxxxx@xxxxx.xxx");
}
ParseFile fileObject = (ParseFile) dealsObject.get("thumbnail");
try {
byte[] imageBytes = fileObject.getData();
sr1.setImage(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
} catch (ParseException e1) {
Log.e("ERROR", e.getMessage());
}
searchResults.add(sr1);
}
listAdapter.notifyDataSetChanged();
welcomeText.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
pDialog.hide();
}
else if (objects.size() == 0)
{
Toast.makeText(getApplicationContext(), R.string.no_search_results, Toast.LENGTH_LONG).show();
pDialog.hide();
}
} else {
Log.d("USERS", "Error: " + e.getMessage());
}
}
});
我的问题是,即使在搜索我知道的用户时,大多数时候它告诉我它找到了0个用户。如果我继续使用相同的搜索字符串运行搜索,可能会有一次从5开始实际返回用户。我遇到的另一个问题是已删除的用户,但搜索仍会找到它们,然后当应用程序尝试从中提取信息时,应用程序会抛出nullPointerException。我的代码有问题吗?
感谢您的时间