简体   繁体   中英

How can I change the value of a static class variable from a method, then get it from an other method?

My problem is that I cannot convert from void to String. I can't use a return in my method onResponse , so I decided to use a global variable, I change it's value from ReadToken method, then I call this method in onResponse , and get the new value of global token_value variable. But I always get null value !

public class TestPagerAdapter extends PagerAdapter {

 static String token_value;

Context context;
List<PagerModel> pagerArr;
LayoutInflater inflater;

public TestPagerAdapter(Context context, List<PagerModel> pagerArr) {
    this.context = context;
    this.pagerArr = pagerArr;
    inflater = ((Activity)context).getLayoutInflater(); }


@Override
public int getCount() {
    return pagerArr.size();
}


@Override
public Object instantiateItem(ViewGroup container, int position) {
    View view= inflater.inflate(R.layout.pop_up,container,false);

     TextView text_task=view.findViewById(R.id.textView8);
    TextView text_child_parent=view.findViewById(R.id.textView9);
     TextView edit_child_parent=view.findViewById(R.id.textView10);
    TextView text_deadline=view.findViewById(R.id.textView12);
     TextView text_avancement=view.findViewById(R.id.textView14);
     Button button=view.findViewById(R.id.button3);
      final Spinner s = view.findViewById(R.id.spinner2);
    ImageView rl=view.findViewById(R.id.imm11);



    view.setTag(position);
    ((ViewPager)container).addView(view);
    final PagerModel model=pagerArr.get(position);
    text_task.setText(model.getText_task());
    text_avancement.setText(model.getText_avancement());
    text_deadline.setText(model.getText_deadline());
    text_child_parent.setText(model.getText_child_parent());
    edit_child_parent.setText(model.getEdit_child_parent());
    button.setText(model.getButton());


    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context,R.array.avancement,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);



    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String d= model.getText_deadline();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(context.getResources().getString(R.string.BaseUrl))
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            final MyInterface myInterface=retrofit.create(MyInterface.class);

             final String st=s.getSelectedItem().toString();
                Call<Success> call22= myInterface.UpdateTask(model.getId(),st);
                call22.enqueue(new Callback<Success>() {
                    @Override
                    public void onResponse(Call<Success> call, Response<Success> response) {
                        Success success = response.body();
                        int s=success.getCode();
                        if(s==0)  Toasty.error(context,"Error", Toast.LENGTH_SHORT).show();

                        else {
                            Toasty.success(context,"Success update",Toast.LENGTH_SHORT).show();

                            String date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
                            ReadToken(model.getEdit_child_parent());

                            Toast.makeText(context, token_value,Toast.LENGTH_SHORT).show();

                            context.startActivity(new Intent(context, MainActivity.class));
                        }
                    }

                    @Override
                    public void onFailure(Call<Success> call, Throwable t) {
                        Toast.makeText(context,"failure",Toast.LENGTH_SHORT).show();

                    }
                });


        }
    });

    return view;
}


@Override
public boolean isViewFromObject(View view, Object object) {
    return view==((View)object);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager)container).removeView((View)object);
}




public void ReadToken(String uid){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(context.getResources().getString(R.string.BaseUrl))
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    final MyInterface myInterface=retrofit.create(MyInterface.class);

    Call<List<User>> call55 = myInterface.readToken(uid);
    call55.enqueue(new Callback<List<User>>() {
        @Override
        public void onResponse(Call<List<User>> call, Response<List<User>> response) {

            List<User> users = response.body();
            String token=users.get(0).getToken();
             token_value =token;
            Toast.makeText(context,"Token ok",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<List<User>> call, Throwable t) {
            Toast.makeText(context,"Token failure",Toast.LENGTH_SHORT).show();

        }
    });
}

}

You read your token in another thread because Retrofit.enqueue() works asynchronously. So, you need to do all the logic with your token inside onResponse() which is inside method ReadToken() .

Store the response in the shared preferences. And use it wherever you want.

SharedPreferences sharedPref = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit(); 
preferencesHelper.editor.putString(“response”, response.toString()).commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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