有 Java 编程相关的问题?

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


共 (4) 个答案

  1. # 1 楼答案

    你们班:

     public class myClassObject implements Serializable {
    
          public myClassObject(){
    
          }
     }
    

    意图传递:

    Intent intent = new Intent(MainActivity.this,NextActivity.class);
        intent.putExtra("question_data", qData);
        intent.putExtra("answer_string", answer);
        startActivity(intent);
    
  2. # 2 楼答案

    一个选项是让您的类实现可序列化接口,然后您可以使用putExtra(Serializable..)在intent extra中传递对象实例Intent#putExtra()方法的变体

    Passing :
       intent.putExtra("MyClassObject", obj);  
    
    To retrive 
    getIntent().getSerializableExtra("MyClassObject");
    
  3. # 3 楼答案

    您需要使用Android Parcelable

    什么是包裹:

    • Parcel is a light weight IPC (Inter Process Communication) data structure, where you can flatten your objects in byte stream.

    • Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable, and to get around some problems with the default Java serialization scheme.

    如何使用Parcelable:

    1.Implement an Interface android.os.Parcelable which will make Objects of Parcelable class.

    2.Overwrite two methods of android.os.Parcelable Interface as bellow :

    • describeContents()- define the kind of object you are going to Parcel.

    • writeToParcel(Parcel dest, int flags)- actual object serialization/flattening happens here. You need to individually Parcel each element of the object.

    3.Define a variable called CREATOR of type Parcelable.Creator

    检查本教程:

    http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html

    可打包与序列化

    • Serialization in Java is far +too slow+ to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects.

    • The problem with Serializable is that it tries to appropriately handle everything under the sun and uses a lot reflection to make determine the types that are being serialized.

  4. # 4 楼答案

    使用可序列化对象,并将其直接保存在Bundle或Intent中

    发送对象

        Intent mIntent=new Intent();
        mIntent.putExtra("iis",new MyClass());
    

    你们班呢

        private class MyClass implements Serializable{
    
    }
    

    到达另一端

    MyClass mc=(MyClass) getIntent().getExtras().getSerializable("iis");
    

    已更新

    也可以将数据作为可包裹对象发送。但是记住

    如果要通过目的向另一个活动发送非基本类型的数据/对象,则必须序列化或实现该对象的Parcelable。首选技术是可包裹的,因为它{}