我的代码在执行完整个过程后,没有任何指令就返回执行先前的方法。

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

My code goes back to execute previous method after executing the whole without any instruction to do that

问题

我在我的安卓代码中遇到了一个奇怪的问题,我在一个活动中有一些方法,一个方法运行然后导致另一个方法运行,但是当最终的方法运行时,之前方法中的代码再次运行,我知道这是因为我在之前的方法中包含了一个Toast,在最终方法完成运行时,应用程序按照预期的方式工作,但是就在最终方法即将完成运行时,我会收到之前方法中的Toast,我确定我没有在最终方法中包含这个Toast。该应用程序没有显示任何错误,除了来自之前方法的不必要的Toast。

package com.example.twelve;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.twelve.Model.Stores;
import com.example.twelve.Prevalent.Prevalent;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.HashMap;

public class Store_Signup extends AppCompatActivity {
    private EditText storename, storemail, storephone, storepassword;
    private TextView Store_login;
    private Button submit_Btn;
    private ProgressBar bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store__signup);

        storename = findViewById(R.id.storename);
        storemail = findViewById(R.id.storeemail);
        storephone = findViewById(R.id.storephone);
        storepassword = findViewById(R.id.storepassword);
        Store_login = findViewById(R.id.Storelogin_text);
        submit_Btn = findViewById(R.id.storesubmit_btn);
        bar = findViewById(R.id.Ssignup_progressbar);

        submit_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String storenametext = storename.getText().toString();
                final String storemailtext = storemail.getText().toString();
                final String storephonetext = storephone.getText().toString();
                final String storepasswordtext = storepassword.getText().toString();
                bar.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(storenametext) || TextUtils.isEmpty(storephonetext) || TextUtils.isEmpty(storemailtext) || TextUtils.isEmpty(storepasswordtext)) {
                    Toast.makeText(Store_Signup.this, "请填写所有字段", Toast.LENGTH_SHORT).show();
                    bar.setVisibility(View.INVISIBLE);
                } else {
                    saveStore(storenametext, storemailtext, storephonetext, storepasswordtext);
                }
            }
        });
    }

    private void saveStore(final String storenametext, final String storemailtext, final String storephonetext, final String storepasswordtext) {
        DatabaseReference storeref = FirebaseDatabase.getInstance().getReference("Stores");
        storeref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.child(storemailtext.replace(".", "")).exists()) {
                    Toast.makeText(Store_Signup.this, "商店电子邮件已存在", Toast.LENGTH_SHORT).show();
                    bar.setVisibility(View.INVISIBLE);
                } else {
                    DatabaseReference stores = FirebaseDatabase.getInstance().getReference("Stores");
                    HashMap<String, Object> storesave = new HashMap<>();
                    storesave.put("storename", storenametext);
                    storesave.put("storemail", storemailtext);
                    storesave.put("storephone", storephonetext);
                    storesave.put("storepassword", storepasswordtext);

                    stores.child(storemailtext.replace(".", "")).setValue(storesave).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                LoginStore(storemailtext, storepasswordtext);
                            } else {
                                bar.setVisibility(View.INVISIBLE);
                                Toast.makeText(Store_Signup.this, "商店未创建,请重试", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    private void LoginStore(final String storemailtext, final String storepasswordtext) {
        if (!TextUtils.isEmpty(storemailtext) && !TextUtils.isEmpty(storepasswordtext)) {

            DatabaseReference storeq = FirebaseDatabase.getInstance().getReference("Stores");
            storeq.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.child(storemailtext.replace(".", "")).exists()) {
                        Stores stores = snapshot.child(storemailtext.replace(".", "")).getValue(Stores.class);
                        if (storepasswordtext.equals(stores.getStorepassword())) {
                            Prevalent.currentOnlineStore = stores;
                            bar.setVisibility(View.INVISIBLE);
                            startActivity(new Intent(Store_Signup.this, Store_Dashboard.class));
                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }
    }
}
英文:

I am facing a strange problem in my android code, I have methods in an activity and a method runs and then leads to another method, but when the final method is ran, codes in the previous method runs again, I know this because I included a Toast in the previous method and when the final method is done running, the app works the way it suppose to, but just when the final method is to finish running, I get the Toast from the previous method which I sure didn't include in the final method. The app does not show any errors and it works fine except the unnecessary Toast from the previous method.

package com.example.twelve;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.example.twelve.Model.Stores;
import com.example.twelve.Prevalent.Prevalent;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
public class Store_Signup extends AppCompatActivity {
private EditText storename, storemail, storephone, storepassword;
private TextView Store_login;
private Button submit_Btn;
private ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store__signup);
storename = findViewById(R.id.storename);
storemail = findViewById(R.id.storeemail);
storephone = findViewById(R.id.storephone);
storepassword = findViewById(R.id.storepassword);
Store_login = findViewById(R.id.Storelogin_text);
submit_Btn = findViewById(R.id.storesubmit_btn);
bar = findViewById(R.id.Ssignup_progressbar);
submit_Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String storenametext = storename.getText().toString();
final String storemailtext = storemail.getText().toString();
final String storephonetext = storephone.getText().toString();
final String storepasswordtext = storepassword.getText().toString();
bar.setVisibility(View.VISIBLE);
if(TextUtils.isEmpty(storenametext) || TextUtils.isEmpty(storephonetext) || TextUtils.isEmpty(storemailtext) || TextUtils.isEmpty(storepasswordtext)){
Toast.makeText(Store_Signup.this, &quot;Please fill out all fields&quot;, Toast.LENGTH_SHORT).show();
bar.setVisibility(View.INVISIBLE);
}else{
saveStore(storenametext, storemailtext, storephonetext, storepasswordtext);
}
}
});
}
private void saveStore(final String storenametext, final String storemailtext, final String storephonetext, final String storepasswordtext) {
DatabaseReference storeref = FirebaseDatabase.getInstance().getReference(&quot;Stores&quot;);
storeref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.child(storemailtext.replace(&quot;.&quot;, &quot;&quot;)).exists()){
Toast.makeText(Store_Signup.this, &quot;Store email already exist&quot;, Toast.LENGTH_SHORT).show();
bar.setVisibility(View.INVISIBLE);
}else{
DatabaseReference stores = FirebaseDatabase.getInstance().getReference(&quot;Stores&quot;);
HashMap&lt;String, Object&gt; storesave = new HashMap&lt;&gt;();
storesave.put(&quot;storename&quot;, storenametext);
storesave.put(&quot;storemail&quot;, storemailtext);
storesave.put(&quot;storephone&quot;, storephonetext);
storesave.put(&quot;storepassword&quot;, storepasswordtext);
stores.child(storemailtext.replace(&quot;.&quot;, &quot;&quot;)).setValue(storesave).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if(task.isSuccessful()){
LoginStore(storemailtext, storepasswordtext);
}else{
bar.setVisibility(View.INVISIBLE);
Toast.makeText(Store_Signup.this, &quot;Store was not created, Please try again&quot;, Toast.LENGTH_SHORT).show();
}
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void LoginStore(final String storemailtext, final String storepasswordtext) {
if(!TextUtils.isEmpty(storemailtext) &amp;&amp; !TextUtils.isEmpty(storepasswordtext)){
DatabaseReference storeq = FirebaseDatabase.getInstance().getReference(&quot;Stores&quot;);
storeq.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.child(storemailtext.replace(&quot;.&quot;, &quot;&quot;)).exists()){
Stores stores = snapshot.child(storemailtext.replace(&quot;.&quot;, &quot;&quot;)).getValue(Stores.class);
if(storepasswordtext == stores.getStorepassword()) {
Prevalent.currentOnlineStore = stores;
bar.setVisibility(View.INVISIBLE);
startActivity(new Intent(Store_Signup.this, Store_Dashboard.class));
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
} 

After the LoginStore() method is done running, i still get the Toast from line 7 of the saveStore() method

Toast.makeText(Store_Signup.this, &quot;Store email already exist&quot;, Toast.LENGTH_SHORT).show();

答案1

得分: 0

我不确定我是否理解正确,但我认为您可能会看到您附加在saveStore上的监听器被多次触发,而您只希望它被触发一次。

如果确实是这种情况,您需要使用addListenerForSingleValueEvent来附加监听器。所以:

private void saveStore(final String storenametext, final String storemailtext, final String storephonetext, final String storepasswordtext) {
    DatabaseReference storeref = FirebaseDatabase.getInstance().getReference("Stores");
    storeref.addListenerForSingleValueEvent(new ValueEventListener() {
        ...
英文:

I'm not sure I understand correctly, but think you might be seeing the listener you attach on saveStore be triggered multiple times, while you only want it to be triggered once.

If that is indeed the case, you'll need to use addListenerForSingleValueEvent to attach the listener. So:

private void saveStore(final String storenametext, final String storemailtext, final String storephonetext, final String storepasswordtext) {
DatabaseReference storeref = FirebaseDatabase.getInstance().getReference(&quot;Stores&quot;);
storeref.addListenerForSingleValueEvent(new ValueEventListener() {
...

huangapple
  • 本文由 发表于 2020年10月2日 05:57:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64163722.html
匿名

发表评论

匿名网友

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

确定