简体   繁体   English

如何在两个不同的类中使用相同的计数器

[英]How to use the same counter in two different classes

I am creating a login fragment where the user can maximum try 3 times to login. 我正在创建一个登录片段,用户最多可以尝试3次登录。

TO do so I wanted to use a counter in the fragment , but given that this fragment also contains an asynctask class for connecting to internet, it also needs to use the counter variable. 为此,我想在fragment使用一个计数器,但是鉴于该片段还包含一个用于连接到Internet的asynctask class ,因此还需要使用counter变量。

I know I could work with SharedPreferences but i was wondering if there was any better approach on it. 我知道我可以使用SharedPreferences但是我想知道是否有更好的方法。

The code would be as follows: 代码如下:

public class loginfragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //CONNECT TO INTERNET!!
                task=new PasswordCheckTask();
                task.execute();
                //END CONNECT TO INTERNET!!
            }
        });
   }

   class PasswordCheckTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPostExecute(Void result) {

            if (success == "1"){
                 Intent xx = new Intent(getContext().getApplicationContext(), ArduinoFirst.class);
                 xx.putExtra("position", position);
                 startActivity(xx);
                 counter=3; //Counter goes back to 3
            } else if (counter!=0) {
                 counter=counter-1 //One less try
            }
       }
 }

How would you do it? 你会怎么做?

您可以使用静态变量在任何地方都可以正常工作

In your example, the PasswordCheckTask class is a subclass of loginfragment (btw, the convention is to name it LoginFragment ). 在您的示例中, PasswordCheckTask类是loginfragment的子类(顺便说一句,习惯LoginFragment其命名为LoginFragment )。 So you could technically define a variable counter of LoginFragment and it can be accessed by both. 因此,您可以从技术上定义LoginFragment的变量counter ,并且两者均可访问它。

However, note that you're dealing with threads here, so you should use something like AtomicInteger or wrap calls to it using the synchronized keyword. 但是请注意,你处理的线程在这里,所以你应该使用类似AtomicInteger或包裹使用它调用synchronized关键字。

Example: 例:

public class LoginFragment extends Fragment {

    private AtomicInteger counter = new AtomicInteger();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //CONNECT TO INTERNET!!
                task=new PasswordCheckTask();
                task.execute();
                //END CONNECT TO INTERNET!!
            }
        });
   }

   class PasswordCheckTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPostExecute(Void result) {

            if (success == "1"){
                 Intent xx = new Intent(getContext().getApplicationContext(), ArduinoFirst.class);
                 xx.putExtra("position", position);
                 startActivity(xx);
                 counter.set(3);
            } else if (counter.get() != 0) {
                 counter.getAndDecrement();
            }
       }
 }

However, I've only seen you use the counter in the subclass PasswordCheckTask so you could have just defined it inside it, unless I've missed something. 但是,我只看到您在子类PasswordCheckTask使用了计数器,因此您可以在其中定义它,除非我错过了什么。

您可以创建一个Utils类,并在其中定义一个public static int变量,您可以从代码的任何地方访问该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM