有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将getBooks()中生成的值(ArrayList<Book>Books变量)放入onCreate方法?

我正在使用函数getBooks在MainActivity中生成Book对象的ArrayList。但我无法将ArrayList放入onCreate,因为每次在onCreate中访问它时,我都会收到一个outofBounds异常,指示我的列表大小为0

另外,如何通过bundle发送此对象列表。我实际上尝试了parcelable,但是在片段的onCreate中,savedinstancestate始终为空,应用程序停止

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();
        }
    });


}

}

共 (1) 个答案

  1. # 1 楼答案

    如果您试图异步获取数据,则getBooks()方法在检索数据之前不会停止。所以你的名单是空的

    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) {
    
            }
        });
    }