1
問題就像標題一樣,我試圖統計用戶(我定義)誰的名字相同,我用countinbackground
或findinbackground
,不管我做了什麼,計數總是0
。有人可以幫助我,我不知道爲什麼。非常感謝。下面是我的代碼:解析查詢不起作用
public boolean checknameunique(String n) {
boolean notexist = true;
ParseQuery<Userdata> query = ParseQuery.getQuery(Userdata.class);
query.whereEqualTo("name", n);
/*query.countInBackground(new CountCallback() {
public void done(int count, ParseException e) {
if (e == null) {
name_count = count;
}
}
});*/
query.findInBackground(new FindCallback<Userdata>() {
@Override
public void done(List<Userdata> objects, ParseException e) {
if(e==null) {
int size = objects.size();
name_count = size;
}
}
});
if(name_count>0) {
notexist = false;
Toast.makeText(this,
"Username already exist",
Toast.LENGTH_LONG).show();
}
return notexist;
}
'findInBackground'是異步的,不需額外'name_count'總是'undefined'?將'Toast'放在'done'塊中,看看我的意思:) – adamdport
將'if(name_count> 0)'塊移到done函數中,然後根據'notexist'的值在裏面做任何你想做的事那裏。 –