java—在一个函数中递增count的值,并在另一个函数中对count使用if条件

wf82jlnq  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(345)

我声明了一个count变量,我想在调用函数时增加count的值,当用户发送到主活动时,count再次变为零,但问题是当调用函数时,值增加了,但在另一个函数中没有增加。当函数被调用时,count的值会增加,但当我在另一个为零的函数中调用它时,count的值保持不变

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;
    }
}
c9x0cxw0

c9x0cxw01#

必须声明count globally public和static,例如: public static int count = 0 . 通过这样做,您可以在另一个函数和另一个类中使用变量计数,方法是键入: SetupActivity.count .

相关问题