简体   繁体   中英

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. 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.

Also how do I send this list of objects through bundle. I actually tried parcelable but the savedinstancestate is always null in the onCreate of the fragment and the application halts.

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. 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) {

        }
    });
}

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