How to return a value from the onResponse of Retrofit function and call it in another procedure?

I am new to modernization and want my getData list to keep the value from the bodyResponse list and call it in another procedure. What is the easiest way to do this? I tried, the data was showing in onResponse, but when I want to use it in another procedure, the getData list value is still null

my onCreate

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // getDataSeason();
    listSeason=new ArrayList<>();
    progress = new ProgressDialog(getActivity());
    progress.setCancelable(false);
    progress.setMessage("Loading. . .");
    progress.show();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(getURL.GetMyURL())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    SeasonAPI api = retrofit.create(SeasonAPI.class);

    Call<SeasonList> call = api.getAllSeason();
    call.enqueue(new Callback<SeasonList>() {
        @Override
        public void onResponse(Call<SeasonList> call, Response<SeasonList> response) {
            progress.dismiss();
            response.body();
            for(int i=0;i<response.body().getResult().size();i++){
                getData.add(i,response.body().getResult().get(i));
            }
        }

        @Override
        public void onFailure(Call<SeasonList> call, Throwable t) {
            t.printStackTrace();
            progress.dismiss();
            Toast.makeText(getActivity(),"Network Connection Error",Toast.LENGTH_SHORT).show();
        }

    });}

String temp

My list getData p>

public class FragmentPromo extends Fragment {

public List<Season> getData;

my season

public class SeasonList {
@SerializedName("value")
@Expose
private Integer value;
@SerializedName("result")
@Expose
private List<Season> result = null;

public Integer getValue() {
    return value;
}

public void setValue(Integer value) {
    this.value = value;
}

public List<Season> getResult() {
    return result;
}

public void setResult(List<Season> result) {
    this.result = result;
}}

Null property when I want to show getData value in oncreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_promo,container,false);
    //Set date in Text View
    txtDate=(TextView)view.findViewById(R.id.txtDateTime);
    txtDate.setText(currendtDateTimeString);

    myRecycleView=(RecyclerView)view.findViewById(R.id.season_view);
    recycleViewAdapterSeason =new RecycleViewAdapterSeason(getContext(),listSeason);
    myRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
    myRecycleView.setAdapter(recycleViewAdapterSeason);
    getDataSeason();
    Toast.makeText(getActivity(),getData.get(0).getNamaSeason(),Toast.LENGTH_SHORT).show();

    return view;
}
+4
source share
1 answer
  • -, Retrofit Singleton, , , .
  • -, onResponse(), response.body() ?
  • ,
  • , , SesaonList , getData​​strong > , getResult,

@Override public void onResponse(Call<SeasonList>call,Response<SeasonList>response){ progress.dismiss(); Seasonlist seasonlist = response.body(); getData = seasonlist.getResult(); }

0

Source: https://habr.com/ru/post/1695905/


All Articles