英文:
Increment the value of count in one function and use if condition on count in another function
问题
package com.codewithosama.socialapp;
public class SetupActivity extends AppCompatActivity {
final static int gallery_pick = 1;
// Here I declare count
int count = 0;
// Remaining code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final String downloadUrl = uri.toString();
// Here I am increasing value of count
usersRef.child("profile image").setValue(downloadUrl)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
startActivity(selfIntent);
Toast.makeText(SetupActivity.this, "Image Stored", Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
count = count + 1;
Log.d("count", String.valueOf(count));
} else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "Error:" + message, Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
}
}
});
}
});
}
});
}
// Remaining Code
private void saveAccountSetupInfo() {
Log.d("count", String.valueOf(count));
String user_name = userName.getText().toString();
String full_name = fullName.getText().toString();
String country = countryName.getText().toString();
// Log.d("uri",ImageUri.toString());
if (TextUtils.isEmpty(user_name)) {
Toast.makeText(this, "Please enter UserName ...", Toast.LENGTH_SHORT).show();
} else if (count == 0) {
Toast.makeText(this, "Please select a profile Image", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(full_name)) {
Toast.makeText(this, "Please enter Full Name ...", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(country)) {
Toast.makeText(this, "Please enter Country ...", Toast.LENGTH_SHORT).show();
} else {
usersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
sendUserToMainActivity();
Toast.makeText(SetupActivity.this, "Your account updated successfully", Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
} else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "Error occurred :" + message, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
// Here I am again assigning value of count zero
private void sendUserToMainActivity() {
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
count = 0;
}
}
英文:
I declare a count variable, I want to increase the value of count when the function is called and count become again zero when user send to Main activity but the problem is when the function is called value is increased but it does not increase in another function. Value of count increases when the function is called but value of count remain same when I call it in another function that is zero
package com.codewithosama.socialapp;
public class SetupActivity extends AppCompatActivity {
final static int gallery_pick = 1;
// Here I declare count
int count = 0;
// Remaining code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final String downloadUrl = uri.toString();
// Here I am increasing value of count
usersRef.child("profile image").setValue(downloadUrl)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
startActivity(selfIntent);
Toast.makeText(SetupActivity.this, "Image Stored", Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
count = count + 1;
Log.d("count", String.valueOf(count));
} else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "Error:" + message, Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
}
}
});
}
});
}
});
}
// Remaining Code
private void saveAccountSetupInfo() {
Log.d("count", String.valueOf(count));
String user_name = userName.getText().toString();
String full_name = fullName.getText().toString();
String country = countryName.getText().toString();
// Log.d("uri",ImageUri.toString());
if (TextUtils.isEmpty(user_name)) {
Toast.makeText(this, "Please enter UserName ...", Toast.LENGTH_SHORT).show();
}
else if (count == 0) {
Toast.makeText(this, "Please select a profile Image", Toast.LENGTH_SHORT).show();
}
else if (TextUtils.isEmpty(full_name)) {
Toast.makeText(this, "Please enter Full Name ...", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(country)) {
Toast.makeText(this, "Please enter Country ...", Toast.LENGTH_SHORT).show();
} else {
usersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
sendUserToMainActivity();
Toast.makeText(SetupActivity.this, "Your account updated successfully", Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
} else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "Error occured :" + message, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
// Here I am again assinging value of count zero
private void sendUserToMainActivity() {
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
count = 0;
}
}
答案1
得分: 1
你必须像这样全局地公开静态地声明count:public static int count = 0
。通过这样做,你可以在另一个函数中以及另一个类中使用变量count,方法是:SetupActivity.count
。
英文:
You must declare count globally public and static like: public static int count = 0
. By Doing this you can use the variable count in another function as well as in another class by typing: SetupActivity.count
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论