简体   繁体   English

我如何获取值(ArrayList <Book> 书籍变量)在我的getBooks()中生成到我的onCreate方法中?

[英]How do I get the values(ArrayList<Book> Books variable) generated in my getBooks() into my onCreate method?

I am generating an ArrayList of Book Objects in my MainActivity using the function getBooks. 我使用函数getBooks在MainActivity中生成Book Objects的ArrayList。 But I am unable to get the ArrayList into my onCreate since everytime I access it in my onCreate I get an outofBounds exeception indicating that my list size is 0. 但我无法将ArrayList放入我的onCreate中,因为每次我在onCreate中访问它时都会得到一个outofBounds异常,表明我的列表大小为0。

Also how do I send this list of objects through bundle. 另外如何通过bundle发送这个对象列表。 I actually tried parcelable but the savedinstancestate is always null in the onCreate of the fragment and the application halts. 我实际上尝试过parcelable但是片段的onCreate中的savedinstancestate总是为null,应用程序停止。

public class MainActivity extends AppCompatActivity implements FragmentA.Communicator{

FragmentA f1;
FragmentB f2;
static ArrayList<Book> b = new ArrayList<Book>();
FragmentManager manager;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getBooks();

    Log.d("Main-Title",b.get(3).getTitle());
    manager = getSupportFragmentManager();
    f1 = (FragmentA) manager.findFragmentById(R.id.fragment);
    f1.setCommunicator(this);

}


@Override
public void respond(int index) {
    f2 = (FragmentB) manager.findFragmentById(R.id.fragment2);
    if(f2!=null && f2.isVisible())
    {
        f2.changeData(index);
    }
    else
    {
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        Fragment newFragment = new FragmentC();
        newFragment.setArguments(bundle);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();


    }
}

private void getBooks(){

    String url = Book.API.BASE_URL;
    //ArrayList<Book> boo;
    Retrofit retrofit =  new Retrofit.Builder().baseUrl(Book.API.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();

    Book.API api = retrofit.create(Book.API.class);

    Call<ArrayList<Book>> call = api.getBooks();
    call.enqueue(new Callback<ArrayList<Book>>() {
        @Override
        public void onResponse(Call<ArrayList<Book>> call, Response<ArrayList<Book>> response) {

            ArrayList<Book> Books = response.body();
            for(Book h: Books){
                Log.d("Title",h.getTitle());
                b.add(h);
            }
        }

        @Override
        public void onFailure(Call<ArrayList<Book>> call, Throwable t) {
            Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
        }
    });


}

}

You are trying to get data asynchronously, The getBooks() method doesn't stop until the data is retrieved. 您试图以异步方式获取数据,getBooks()方法在检索数据之前不会停止。 so your list is empty. 所以你的清单是空的。

public void onCreate(){
    getBooks();
    //here b might be empty
}
public void afterGettingBooks(){
    //here b will have retrieved data
}
private void getBooks(){
    //code
    call.enqueue(new Callback<ArrayList<Book>>() {
        @Override
        public void onResponse(Call<ArrayList<Book>> call, Response<ArrayList<Book>> response) {
            //upadte b
            afterGettingBooks();
            }
        }
        @Override
        public void onFailure(Call<ArrayList<Book>> call, Throwable t) {

        }
    });
}

暂无
暂无

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

相关问题 我的Arraylist在oncreate中为null - My Arraylist is null in oncreate 如何将我的代码分成不同的文件,并在android中的onCreate方法中调用它们 - How do I separate my code into different files and call them within the onCreate method in android 如何使我的Java ArrayList正确显示? - How do I get my java ArrayList to display properly? 当我将其输入到另一个arrayList中时,如何修改我的arrayList,以便删除并更改它的值? - How do I modify my arrayList as I enter it into another arrayList such that to remove and change it's values? 如何在 onCreate 方法之外初始化我的 RelativeLayoutButton? - How can I initialize my RelativeLayoutButton outside of onCreate method? 我如何获得数组列表中我的哈希图的副本 - How do i get a clone of my hashmap that is in an arraylist 如何从“ onCreate()”访问同一类的另一个方法中的Arraylist变量? - How to access from “onCreate()” an Arraylist variable in another method of same class? 将我的用户输入传递给方法时,如何获取用户输入以访问我的ArrayList - How can I get my userinput to access my ArrayList when I pass it to a method 如何在我的数组列表中找到一个元素? - How do I locate a element in my arraylist? 当我希望线程共享ArrayList时,应在哪里实例化ArrayList,以及如何从main方法中访问它? - When I want my threads to share an ArrayList, where do I instantiate the ArrayList, and how do I access it from the main method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM