Hello i am trying to create a class called ParcelableObject in which i implement the Parcelable interface.
Then i will create other objects, which they will extend ParcelableObject.
I am doing it because i don't want to write the methods of Parcelable interface in each object.
As for now i have managed to do this for Objects containing other Objects (which all of them extend the ParcelableObject class) and for primitive Types.
I am having troubles doing it for Arrays and List of primitive Types and of course Arrays and Lists of Objects
I intend to expand this to cover Lists, Arrays.
The way i did it is by getting all the fields of the object given
So here is my question.
First of all
Is this possible?
Second i tried to expand it for arrays or lists of primitive types and i fail to do it.
To be more specific. Now i am trying to expand it to Arrays of Integers, or Lists of Integers.
As i see i fail to read from the parcel a List of Integers because i get cast errors etc.
So any help would be grateful.
Also the basic idea around this is to store to the ParcelableObject the object of its subclass.
Then if i get an object (not primitive type) i write its class name to parcel and then the object.
So when i want to read an object from the parcel, i read first the string with its class name, make a new instance of the class using (Class.forName() etc) and then assign the object read from parcel to the Object instantiated above. If there is any better way to get the Class Name of the ParcelableObject's subclass other than filling the parcel unnecessary Strings id like to know.
Here is the ParcelableObject.java
You can download the whole android ParcelableObject project to test it. Demonstration is included.
Tested it under virtual device Nexus S (4.2). Project is targeted to work for 4.0 and above.
https://github.com/tchar/ParcelableObject/blob/master/ParcelableObject/src/com/parcelableobject/ParcelableObject.java
UPDATE*** Added Support for primitive Object Lists.
Still having problems with primitive types (int, etc) not with Integers and list of ParcelableObjects
Thanks in advance.
Related
I have exported an ArrayList of instances of class defined by myself, and I need to read in another project. I don't intend to use the methods of that class in particular, only to use its atributes.
I could transform those atribuites into ArrayList of primitive objects, that I could read afterwards without the need of having that class implemented again in that project, but I'm specifically asked not to do so. Is there a way to get only the atributes of such class?
I am looking for an answer to the questions
why constructors are not necessary for primitive types in java?
is it because primitive types do not need to be built before use?
First, you must understand that primitive types are not objects. They've been made to help C users switch to Java. Also, primitive types do not store any instance variables like objects do, but are rather values themselves.
It's also worth noting that you also don't need to call constructor of any class that extends interface Number (Integer, Double, Long, etc.).
There are some gotchas in Java that sometimes make it feel like it's not entirely like that, but you should mostly be fine using ethier primitive types or objects in java (except for, that primitive types don't have any methods, so you will need to turn int into Integer with Integer.valueOf(n) before you will be able to, for example, call toString())
a constructor is a special method wich purpose is to create a new instance of and object, since primitive data types are not objects they don't have a constructor or methods associated to them.
However Java has primitive wrapper class for some primitive datatypes.
the wrapper class are very useful in the case of the Integer class you can use methods like parseInt(String s), toBinaryString(int i), or to do some operations over a Integer object
i.e:
Integer i = new Integer(7);
byte b = i.byteValue();
Primitive types
Declaring a primitive type e.g. int i; you can see that it is already in life (i.e. the space in memory is already taken),even if it doesn't have a value assigned i.e. even if it is not defined.
Objects
First of all, an object is a abstract concept consisting of state,behaviour and identity.
State: information encapsuled in the object
Behaviour: defined by method written within the class of which the object is instance
Identity: despite from primitive types that are equal if they have the same value, two objects are not the same object neither if they have the same state, i.e. if they encapsulate the same information.
So to give life to an object, declaration is not enough, it needs also the definition i.e. the call to a costructor.
The indefinite article is important: in fact an object can have many different costructors with different signatures, providing different ways to initialize it. So, different costuctors lead to differents state and behaviours, and this the reason why an object needs to be built.
At university we have studied that "ADT combines the same type of data when it is not confined with just one data type perhaps it can take other data types too. So this structure is called generic structure." and I don't know what on earth does that mean???
Based on my interpretation of that sentence, it means that ADTs can be adapted to store different types of objects. In Java, ADTs such as TreeSets can store any type that extends Object (so basically no primitives). You can have TreeSet<Integer>, TreeSet<String> and even TreeSet<CustomType>.
I am looking to pass an array of object from activity to activity in Android. I understand that the optimal way to do this is having your passed objects implement Parcelable or Serializable, as explained here.
However, if I am passing an array of these objects, will this still work, considering you are now passing an array of objects that implement those interfaces, rather than the object itself? If not, would I instead need to extend a class such as ArrayList and implement these interfaces, and pass that 'array' object instead?
As I understand it, calling serialize on an array causes the array to recursively call serialize on its members. However, it appears there may be a bug affecting this in Android versions prior to 5.0.1 https://stackoverflow.com/a/28720450/1541763
It seems that parceling an array follows this logically, but that unmarhsalling is a little more complicated: https://stackoverflow.com/a/10781119/1541763
These questions are purely asked out of curiosity. I don't actually need to subclass an array, I'm just trying to figure out more about how they work in Java.
Where is the Javadoc API for arrays? I found one for the 'Arrays' class, but that class just contains utilities to use on Java arrays, and is not the actual array class. This leads me to my next question:
IS there an actual array class of which all arrays are subclasses?
Is Object[] a superclass of String[] (for example)? I'm guessing the answer here is no. Are these actual classes like any other class?
Is String[] a different class from String[][]? Or String[][][], etc?
As asked in the title, is it possible to subclass an array class (or to subclass THE array class? still not sure how it works as you can tell by my above questioning)? Could I create my own class, instances of which acted exactly like arrays, except they had more functionality?
Thanks.
The Java Language Specification answers all these questions:
The direct superclass of an array type is Object.
Every array type implements the interfaces Cloneable and java.io.Serializable.
So no, there isn't a common base class for all arrays, and therefore no JavaDoc either. The members of arrays are defined by the spec instead.
Subtyping among array types is defined in section 4.10.3 - and yes, String[] is a subtype of Object[]. See also ArrayStoreException.
Yes, String[].class != String[][].class. (c.f. section 10.8)
No, there is no syntax to subclass arrays. In particular, the extends clause must contain a class type (and array types are not class types).
There is no array class in Java.
Interestingly, arrays are objects (but not class instances):
An object is a class instance or an array.
More here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
The classes implementing java.util.List provide a more object-oriented implementation of array-like structures.
You can't subclass arrays. Even though the syntax used with them is a bit different, they are objects (check with the JLS). There's not much API to them - apart from just what Object has (with toString() not doing what you expect, use Arrays.deepToString() for that; equals() and hashCode() are similar) there's the length field. Additionally, arrays are cloneable. You can only cast array types if the target element type is a supertype of the source element type - you can cast String[] to Object[] but not the other way around. If you are sure the objects in the array are a specific type, you can cast each element individually. String[][] is an array of String[], so it's a different type than String[] as its elements are arrays of String, not Strings. You can create classes which give similar functionality to arrays (ArrayList does just that), but they will not be interchangeable with regular arrays.