第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

無(wú)法在 call.enqueue 中的 for 循環(huán)之外獲取 ArrayList

無(wú)法在 call.enqueue 中的 for 循環(huán)之外獲取 ArrayList

哈士奇WWW 2023-02-23 17:30:43
我想通過(guò) Retrofit 中的 call.enqueue 方法從 for 循環(huán)中獲取 ArrayList 數(shù)據(jù)。如何訪(fǎng)問(wèn) call.enqueue 方法之外的列表?一切正常。打印列表大小時(shí),我得到了我想要的價(jià)值。唯一的問(wèn)題是我無(wú)法從 call.enqueue 方法之外訪(fǎng)問(wèn)值。private void getSchoolList() {    final Call<List<DanceSchool>> call = RetrofitClient.getInstance().getApi().getDanceSchools();    call.enqueue(new Callback<List<DanceSchool>>() {        @Override        public void onResponse(Call<List<DanceSchool>> call, Response<List<DanceSchool>> response) {            if(!response.isSuccessful()) {                Toast.makeText(ListActivity.this, "Response Code: " + response.code(), Toast.LENGTH_SHORT).show();            }            List<DanceSchool> danceSchools = response.body();            for(DanceSchool danceSchool:danceSchools){                schoolNameList.add(danceSchool.getSchool_name());                dayList.add(danceSchool.getDays());                timingList.add(danceSchool.getSun_timing());                contactNoList.add(danceSchool.getContact_no());                addressList.add(danceSchool.getAddress());            }        }        @Override        public void onFailure(Call<List<DanceSchool>> call, Throwable t) {            Toast.makeText(ListActivity.this, "Failure: "+t.getMessage(), Toast.LENGTH_SHORT).show();        }    });}我想在 call.enqueue 方法之外訪(fǎng)問(wèn) ArrayList。
查看完整描述

2 回答

?
慕無(wú)忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

最簡(jiǎn)單的方法可能是聲明一個(gè)函數(shù),


void accessArrayList(ArrayList<DanceSchool> danceSchool){

    //do stuff with the values

}

并在里面調(diào)用它c(diǎn)all.enqueue


call.enqueue(new Callback<List<DanceSchool>>() {

    @Override

    public void onResponse(....) {

        ........

        List<DanceSchool> danceSchools = response.body();

        accessArrayList(danceSchools);

        .......

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-02-23
?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

重點(diǎn)是:現(xiàn)在您正在進(jìn)行異步調(diào)用。您正在請(qǐng)求一些信息,當(dāng)該信息已編譯并準(zhǔn)備就緒時(shí),onResponse()將調(diào)用該方法。只有這樣,該列表才能從傳入的響應(yīng)正文中填充。


因此,您可以做的是:在您的封閉類(lèi)中有另一個(gè)方法,例如updateList(),然后簡(jiǎn)單地從您的onResponse()實(shí)現(xiàn)中調(diào)用該方法。


除此之外,您可能希望將異步調(diào)用變成同步調(diào)用。然后,相反,您使用execute()等待結(jié)果進(jìn)來(lái)(例如,請(qǐng)參見(jiàn)此處)。但是你仍然需要有一個(gè)回調(diào)方法來(lái)調(diào)用。


或者,您可以在封閉類(lèi)中有一個(gè)字段,例如


List<DanceSchool> danceSchools

...

private void getSchoolList() {

  final Call<List<DanceSchool>> call = ...    

  call.enqueue(new Callback<List<DanceSchool>>() {

    @Override

    public void onResponse(Call<List<DanceSchool>> call, Response<List<DanceSchool>> response) {

    ...

    danceSchools.addAll(response.body());


查看完整回答
反對(duì) 回復(fù) 2023-02-23
  • 2 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)