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

Not able to send an Arraylist of objects using putparcelablearraylist to the fragment through bundle?

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

Bundle 接受了参数,但是当我尝试接收片段中对象的数组列表时,我得到的 savedinstance 状态值为 null。

以下是我的 MainActivity 的代码:-

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

以下是自定义对象的代码:

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


}
}

发件人class

Bundle bundle = new Bundle();
bundle.putSerializable("bookarray",(ArrayList<Book>)b);

接收者class

book_a = (ArrayList<Book>)getArguments().getSerializable("bookarray");

您正在添加捆绑包,然后设置 f1。试试这个方法。

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 = (FragmentA) manager.findFragmentById(R.id.fragment);
    f1.setArguments(bundle);

    f1.setCommunicator(this);
}

您还在两个地方创建片段。您应该只在填充数组后创建它。

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

    getBooks();
}

您还应该始终在访问 savedInstanceState 之前检查是否为 null。

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

    return view;
}