Unchecked converts java.io.Serializable to java.util.ArrayList
Please help, I get the following message in the following code:
listaFinal = (ArrayList<PuntoNota>) getIntent().getSerializableExtra("miLista");
AdapterDatos adapter = new AdapterDatos(this, listaFinal);
PuntoNota.java
public class PuntoNota implements Serializable{
private String punto;
private String nota;
public PuntoNota (String punto, String nota){
this.punto = punto;
this.nota = nota;
}
public String getPunto(){
return punto;
}
public String getNota(){
return nota;
}
}
Adapter data:
public AdapterDatos(Context context, ArrayList<PuntoNota> puntoNotaList) {
this.context = context;
this.puntoNotaList = puntoNotaList;
}
The application works fine, but I get the following message:
Unchecked cast: ‘java.io.Serializable’ to ‘java.util.ArrayList ‘ less … (Ctrl + F1).
about this code: (ArrayList ) getIntent (). getSerializableExtra (“myList”); will it be advisable to delete or hide this message?
Solution
Root cause: This is a warning from the IDE, getSerializableExtra
returns Serializable
, and you are trying to convert to ArrayList<PuntoNota>
. If the program cannot convert it to the type you expect, it may throw a ClassCastException at runtime.
Solution: To pass a user-defined object in Android, your class should implement a Parcelable
instead of a Serializable
interface.
class PuntoNota implements Parcelable {
private String punto;
private String nota;
public PuntoNota(String punto, String nota) {
this.punto = punto;
this.nota = nota;
}
protected PuntoNota(Parcel in) {
punto = in.readString();
nota = in.readString();
}
public String getPunto() {
return punto;
}
public String getNota() {
return nota;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(punto);
dest.writeString(nota);
}
public static final Creator<PuntoNota> CREATOR = new Creator<PuntoNota>() {
@Override
public PuntoNota createFromParcel(Parcel in) {
return new PuntoNota(in);
}
@Override
public PuntoNota[] newArray(int size) {
return new PuntoNota[size];
}
};
}
Sender
ArrayList<PuntoNota> myList = new ArrayList<>();
Fill data to myList here
...
Intent intent = new Intent();
intent.putParcelableArrayListExtra("miLista", myList);
On the receiving end
ArrayList<? extends PuntoNota> listaFinal = getIntent().getParcelableArrayListExtra("miLista");