无法通过捆绑包将使用putparcelablearraylist的对象的Arraylist发送到片段? [英] Not able to send an Arraylist of objects using putparcelablearraylist to the fragment through bundle?

查看:60
本文介绍了无法通过捆绑包将使用putparcelablearraylist的对象的Arraylist发送到片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用putparcelablearraylist通过捆绑包将对象的Arraylist发送到片段.但是我将saveinstancestate的值获取为null,因此无法检索列表值.我的实现有什么问题吗?

I have been trying to send an Arraylist of objects using putparcelablearraylist to the fragment through bundle. But I am getting the value of savedinstancestate as null and I am hence not able to retrieve the list values. Is there anything wrong with my implementation??

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

Bundle is taken the parameters but when I am trying to receive the arraylist of objects in the fragment, I am getting the value of the savedinstance state as null.

以下是我的MainActivity的代码:-

Following is the code for my 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();
        }
    });


}
}

以下是接收片段的代码:

Following is the code for the receiving fragment:

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

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

Following is the code for the Custom Object:

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


}
}

推荐答案

发件人类

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

接收器类

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

这篇关于无法通过捆绑包将使用putparcelablearraylist的对象的Arraylist发送到片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆