我使用asynctask但我無法處理doInBackground方法中的局部變量,所以在我的情況下,當我想設置局部變量測試時出現錯誤 那麼我該怎麼辦? 當我用這種方式設置測試test =「testme」沒有這個我沒有得到一個錯誤,但測試總是爲空。 所以我能做些什麼來設置一個局部變量doinbackground方法調用doInbackground中的局部變量
public class ConnectTask extends AsyncTask{
Context context;
User user2 ;
String test;
private User authentifcatedUser;
public ConnectTask(User authentifcatedUser) {
this.authentifcatedUser = authentifcatedUser;
}
public User getAuthentifcatedUser() {
return authentifcatedUser;
}
public void setAuthentifcatedUser(User authentifcatedUser) {
this.authentifcatedUser = authentifcatedUser;
}
public String getTest() {
return test;
}
public User getUser(){
return this.user2;
}
public void setTest(String test) {
this.test = test;
}
@Override
protected Object doInBackground(Object[] params) {
connect();
return test;
}
public String connect(){
final Firebase ref = new Firebase("https://app.firebaseio.com/users");
Query query = ref.orderByChild("username").equalTo("testme");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
User user = userSnapshot.getValue(User.class);
System.out.println("my birth day is "+user.getDateOfBirth());
user2=new User(user.getUsername(),user.getEmail(),user.getPassword(),user.getSignUpDate(),user.dateOfBirth);
System.out.println("my email is" + user2.getEmail());
test="aaaaaaaaa";
}
/* User user = dataSnapshot.getValue(User.class);
Log.w("tag", dataSnapshot.toString());*/
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
return test;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
System.out.println("retuuuurn"+connect()) ;
}
}
你想要什麼?你是否想說'test =「aaaaaaaaa」;'是不會設置'test'的值? –
是的,我想設置測試值 – fatma19
參考@Sohail Zahid的答案在下面..這是正確的 –