android - 如何在 fragment 中使用 Parcelable 获取数据?

标签 android fragment parcelable

我可以在 Activity 中使用 Parcelable,但我不知道如何在 Fragment 中使用它。

我在 FragmentGet 中有一个 ListFragment 来显示 ListView 中的所有行数据库,我想在单击另一个 Fragment 时获取 ListView 的详细信息,我使用 Parcelable 在 Fragment 之间传递数据。

这是我的 Test.java:

import android.os.Parcel;
import android.os.Parcelable;

public class Test implements Parcelable {

    private int studentNumber;
    private String studentName;
    private String studentFamily;

    public int getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(int studentNumber) {
        this.studentNumber = studentNumber;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getStudentFamily() {
        return studentFamily;
    }

    public void setStudentFamily(String studentFamily) {
        this.studentFamily = studentFamily;
    }

    public Test() {
    }

    public Test(Parcel read) {
        studentNumber = read.readInt();
        studentName = read.readString();
        studentFamily = read.readString();
    }

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

    @Override
    public void writeToParcel(Parcel write, int flags) {
        write.writeInt(studentNumber);
        write.writeString(studentName);
        write.writeString(studentFamily);
    }

    public static final Parcelable.Creator<Test> CREATOR =
            new Parcelable.Creator<Test>() {
                @Override
                public Test createFromParcel(Parcel source) {
                    return new TestP(source);
                }
                @Override
                public Test[] newArray(int size) {
                    return new TestP[size];
                }
            };
}

在FragmentSend.java中我想发送数据到FragmentGet.java,这是代码:

public class FragmentSend extends ListFragment {

    //    Classes
    TestReadData readData;
    TestDataSource dataSource;

    private List<Test> listTest;       

    public FragmentSend() {
        // Required empty public constructor
    }


    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View layout = inflater.inflate(R.layout.fragment_send, container, false);

        readData = new TestReadData(getContext());
        dataSource = new TestDataSource(getContext());  

       refreshStudentList(inflater);

    return layout;
}

public void refreshStudentList(LayoutInflater inflater) {

    listTest = readData.getAllStudentRows();
    ArrayAdapter<Test> testAdapter = new TestFragmentListAdapter(inflater.getContext(), listTest);
    setListAdapter(testAdapter);

}

    @Override
    public void onResume() {
        super.onResume();
        dataSource.open();
    }

    @Override
    public void onPause() {
        super.onPause();
        dataSource.close();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Test model = listTest.get(position);

        // this three line code is for Activity not Fragment!
        Intent intent = new Intent(getContext(), FragmentGet.class);
        intent.putExtra("Student", model);
       // startActivity(intent);

        // I use this code for fragment
        Fragment fragmentGet = new FragmentGet();
    fragmentGet.setArguments(intent.getExtras());
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frame_container, fragmentGet);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

    }

在 FragmentGet.java 中我得到这样的数据:

public class FragmentGet extends Fragment {

    //    Classes
    Test model;    

    //    Variables
    private List<Test> listTest;

    public FragmentGet () {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout = inflater.inflate(R.layout.fragment_get, container, false);

        //        Classes Associated
        model = new Test(Parcel.obtain());

        Bundle bundle = getActivity().getIntent().getExtras();
        model = bundle.getParcelable("Student");

        return layout;    
    }

当我运行该应用程序时,我会收到此错误:

Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference

我该如何解决这个错误?

最佳答案

将数据传递给 Fragments 是通过使用 Bundles 而不是 Intents 进行的。

FragmentSend中改变

Intent intent = new Intent(getContext(), FragmentGet.class);
intent.putExtra("Student", model);

Fragment fragmentGet = new FragmentGet();
fragmentGet.setArguments(intent.getExtras());

对此

Fragment fragmentGet = new FragmentGet();
Bundle bundle = new Bundle();
bundle.putParcelable("Student", model);
fragmentGet.setArguments(bundle);

并在FragmentGet中接收数据变化

Bundle bundle = getActivity().getIntent().getExtras();
model = bundle.getParcelable("Student");

Bundle bundle = this.getArguments();
if (bundle != null) {
    model = bundle.getParcelable("Student");
}

关于android - 如何在 fragment 中使用 Parcelable 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645719/

相关文章:

android - 适配器类中的 RecyclerView 多个布局 View

android - FragmentStatePagerAdapter 性能问题

android - 在 Activity 中查找 fragment View 。 Android findFragmentByTag(String tag) 返回 null

javascript - Safari 将 URL # 片段合并到其浏览器缓存中

java - 尝试读取包裹 ArrayList<Object> 时出现 NullPointerException

java - Parcelable 协议(protocol)要求该类实现 Parcelable

java - Android 中用于双击空格的自定义键盘输入过滤器

java - 在 Android 中使用整数 ArrayList

android - 我应该实现 writeToParcel 方法吗?

java - Android图片设置高度问题