Java 无法使用putparcelablearraylist通过bundle将对象的Arraylist发送到片段?

Java 无法使用putparcelablearraylist通过bundle将对象的Arraylist发送到片段?,java,android,android-fragments,parcelable,Java,Android,Android Fragments,Parcelable,我一直在尝试使用putparcelablearraylist通过bundle将对象的Arraylist发送到片段。但是我得到的savedinstancestate值为null,因此无法检索列表值。我的实现有什么问题吗 Bundle接受参数,但当我试图接收片段中对象的arraylist时,我得到的savedinstance状态值为null 以下是我的主要活动的代码:- public class MainActivity extends AppCompatActivity implements Fr

我一直在尝试使用putparcelablearraylist通过bundle将对象的Arraylist发送到片段。但是我得到的savedinstancestate值为null,因此无法检索列表值。我的实现有什么问题吗

Bundle接受参数,但当我试图接收片段中对象的arraylist时,我得到的savedinstance状态值为null

以下是我的主要活动的代码:-

public class MainActivity extends AppCompatActivity implements FragmentA.Communicator{

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




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

    getBooks();

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


    }
}

public void afterGetBooks(ArrayList<Book> bks) {
    for (Book h : bks) {
        b.add(h);
    }
    manager = getSupportFragmentManager();
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("bookarray",(ArrayList<Book>)b);
    f1.setArguments(bundle);
    f1 = (FragmentA) manager.findFragmentById(R.id.fragment);
    f1.setCommunicator(this);


}




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

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


}
}
public类MainActivity扩展AppCompatActivity实现FragmentA.Communicator{
片段f1;
片段b-f2;
ArrayList b=新的ArrayList(7);
碎片管理器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getBooks();
manager=getSupportFragmentManager();
f1=(FragmentA)manager.findFragmentById(R.id.fragment);
f1.设置通讯器(本);
}
@凌驾
公共无效响应(整数索引){
f2=(FragmentB)manager.findFragmentById(R.id.fragment2);
如果(f2!=null&&f2.isVisible())
{
f2.变更数据(索引);
}
其他的
{
Bundle=新Bundle();
bundle.putInt(“索引”,索引);
Fragment newFragment=newfragmentc();
setArguments(bundle);
FragmentTransaction=getSupportFragmentManager().beginTransaction();
事务。替换(R.id.fragment,newFragment);
transaction.addToBackStack(空);
commit();
}
}
公开作废afterGetBooks(ArrayList bks){
适用于(手册h:bks){
b、 加(h);
}
manager=getSupportFragmentManager();
Bundle=新Bundle();
bundle.putParcelableArrayList(“bookarray”,(ArrayList)b);
f1.设置参数(bundle);
f1=(FragmentA)manager.findFragmentById(R.id.fragment);
f1.设置通讯器(本);
}
私人书籍{
字符串url=Book.API.BASE\u url;
//ArrayList boo;
改装改装=新建改装.Builder().baseUrl(Book.API.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
Book.API=reformation.create(Book.API.class);
Call Call=api.getBooks();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
ArrayList Books=response.body();
(h册:书籍){
Log.d(“Title”,h.getTitle());
//b、 加(h);
}
课后辅导书;
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
以下是接收片段的代码:

public class FragmentA extends Fragment implements AdapterView.OnItemClickListener{

ListView list;
Communicator communicator;
ArrayList<Book> book_a;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.fragment_a,container,false);
    savedInstanceState = getArguments();
    book_a = savedInstanceState.getParcelableArrayList("bookarray");
    Log.d("Frag_a:Title",book_a.get(5).getTitle());
    list= (ListView) view.findViewById(R.id.listview);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),R.array.chapters,android.R.layout.simple_list_item_1);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);

    return view;
}

public void setCommunicator(Communicator communicator)
{
    this.communicator = communicator;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    communicator.respond(position);

}

public interface Communicator{

    public void respond(int index);
}
}
公共类FragmentA扩展片段实现AdapterView.OnItemClickListener{
列表视图列表;
沟通者;
ArrayList book_a;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(右布局。碎片a,容器,假);
savedInstanceState=getArguments();
book_a=savedInstanceState.getParcelableArrayList(“bookarray”);
Log.d(“Frag_a:Title”,book_a.get(5.getTitle());
list=(ListView)view.findViewById(R.id.ListView);
ArrayAdapter=ArrayAdapter.createFromResource(getActivity(),R.array.chapters,android.R.layout.simple_list_item_1);
list.setAdapter(适配器);
list.setOnItemClickListener(此);
返回视图;
}
公共通讯器(通讯器)
{
this.communicator=通信器;
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
通讯员。应答(位置);
}
公共接口通信器{
公共空间响应(int索引);
}
}
以下是自定义对象的代码:

public class Book implements Parcelable{
String title,author,coverURL;
int id, published;


public Book(String title, String author, String coverURL, int id, int published) {

    this.id = id;
    this.title = title;
    this.author = author;
    this.published = published;
    this.coverURL = coverURL;

}

protected Book(Parcel in) {
    id = in.readInt();
    title = in.readString();
    author = in.readString();
    published = in.readInt();
    coverURL = in.readString();
}

public static final Creator<Book> CREATOR = new Creator<Book>() {
    @Override
    public Book createFromParcel(Parcel in) {
        return new Book(in);
    }

    @Override
    public Book[] newArray(int size) {
        return new Book[size];
    }
};

public String getTitle() {

    return title;
}

public String getAuthor() {
    return author;
}

public String getCoverURL() {
    return coverURL;
}

public int getId() {
    return id;
}

public int getPublished() {
    return published;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(title);
    dest.writeString(author);
    dest.writeInt(published);
    dest.writeString(coverURL);
}

public interface API{

    String BASE_URL = "https://kamorris.com/lab/audlib/";

    @GET("booksearch.php")
    Call<ArrayList<Book>> getBooks();


}
}
public类书实现可包裹{
字符串标题、作者、封面URL;
int id,已出版;
公共书籍(字符串标题、字符串作者、字符串封面URL、int id、int published){
this.id=id;
this.title=标题;
this.author=作者;
this.published=published;
this.coverURL=coverURL;
}
受保护书籍(包裹内){
id=in.readInt();
title=in.readString();
author=in.readString();
published=in.readInt();
coverURL=in.readString();
}
公共静态最终创建者=新创建者(){
@凌驾
公共图书createFromParcel(地块中){
归还新书(中文版);
}
@凌驾
公共图书[]新数组(整数大小){
归还新书[大小];
}
};
公共字符串getTitle(){
返回标题;
}
公共字符串getAuthor(){
返回作者;
}
公共字符串getCoverURL(){
返回coverURL;
}
公共int getId(){
返回id;
}
public int getPublished(){
公布报税表;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地写入(id);
目的书面记录(标题);
目的地书面限制(作者);
目的书(已出版);
目的地writeString(coverURL);
}
公共接口API{
字符串BASE_URL=”https://kamorris.com/lab/audlib/";
@获取(“booksearch.php”)
调用getBooks();
}
}

发送者类

Bundle bundle = new Bundle();
bundle.putSerializable("bookarray",(ArrayList<Book>)b);
book_a = (ArrayList<Book>)getArguments().getSerializable("bookarray");
Bundle Bundle=新Bundle();
bundle.putSerializable(“bookarray”,(ArrayList)b);
接收器类别

Bundle bundle = new Bundle();
bundle.putSerializable("bookarray",(ArrayList<Book>)b);
book_a = (ArrayList<Book>)getArguments().getSerializable("bookarray");
book_a=(ArrayList)getArguments().getSerializable(“bookarray”);

发送者类

Bundle bundle = new Bundle();
bundle.putSerializable("bookarray",(ArrayList<Book>)b);
book_a = (ArrayList<Book>)getArguments().getSerializable("bookarray");
Bundle Bundle=新Bundle();
bundle.putSerializable(“bookarray”,(ArrayList)b);
接收器类别

Bundle bundle = new Bundle();
bundle.putSerializable("bookarray",(ArrayList<Book>)b);
book_a = (ArrayList<Book>)getArguments().getSerializable("bookarray");
book_a=(ArrayList)getArguments().getSerializable(“bookarray”);

您正在添加捆绑包,然后设置f1。这样试试看