繁体   English   中英

如何从一个方法更改静态类变量的值,然后从另一个方法获取它呢?

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

我的问题是我无法从void转换为String。 我不能在方法onResponse中使用return,所以我决定使用全局变量,我从ReadToken方法更改了它的值,然后在onResponse中调用了此方法,并获得了全局token_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();

        }
    });
}

}

您在另一个线程中读取令牌,因为Retrofit.enqueue()是异步工作的。 因此,您需要在方法ReadToken()内的onResponse()对令牌进行所有逻辑处理。

将响应存储在共享首选项中。 随时随地使用它。

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

暂无
暂无

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

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