增加一个函数中count的值,并在另一个函数中对count使用if条件。

huangapple go评论61阅读模式
英文:

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&lt;UploadTask.TaskSnapshot&gt;() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener&lt;Uri&gt;() {
@Override
public void onSuccess(Uri uri) {
final String downloadUrl = uri.toString();
// Here I am increasing value of count
usersRef.child(&quot;profile image&quot;).setValue(downloadUrl)
.addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()) {
Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
startActivity(selfIntent);
Toast.makeText(SetupActivity.this, &quot;Image Stored&quot;, Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
count = count + 1;
Log.d(&quot;count&quot;, String.valueOf(count));
} else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, &quot;Error:&quot; + message, Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
}
}
});
}
});
}
});
}
// Remaining Code
private void saveAccountSetupInfo() {
Log.d(&quot;count&quot;, String.valueOf(count));
String user_name = userName.getText().toString();
String full_name = fullName.getText().toString();
String country = countryName.getText().toString();
// Log.d(&quot;uri&quot;,ImageUri.toString());
if (TextUtils.isEmpty(user_name)) {
Toast.makeText(this, &quot;Please enter UserName ...&quot;, Toast.LENGTH_SHORT).show();
}
else if (count == 0) {
Toast.makeText(this, &quot;Please select a profile Image&quot;, Toast.LENGTH_SHORT).show();
}
else if (TextUtils.isEmpty(full_name)) {
Toast.makeText(this, &quot;Please enter Full Name ...&quot;, Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(country)) {
Toast.makeText(this, &quot;Please enter Country ...&quot;, 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, &quot;Your account updated successfully&quot;, Toast.LENGTH_SHORT)
.show();
loadingBar.dismiss();
} else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, &quot;Error occured :&quot; + 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.

huangapple
  • 本文由 发表于 2020年9月3日 19:16:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63722501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定