Reputation: 785
I have 2 classes. In first class, I write a data from List, in the second class, I want read this data and put into listview. It save a data correctly, but when I try to get it, logcat give a error:
java.lang.ClassCastException: pl.pawelfrydrych.CHAPS.AdapterPlanuProbOnline$podzialPlanu cannot be cast to pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline$podzialPlanu
at pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline$AdapterDlaPlanu.getView(AdapterPlanuProbOffline.java:115)
I can't understand why, because always I get the same type of data.
First class write method:
public void SaveAsFile(List<podzialPlanu> lista){
// lista - List<podzialPlanu> podzialPlanuList2 = new ArrayList<podzialPlanu>();
try {
FileOutputStream saveFile = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + "planProby.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(lista);
save.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
And the main class, when I try to get data from this file, and put it into my listview
public class AdapterPlanuProbOffline extends Activity implements Serializable {
List<String> DzienDataList;
List<String> GodzinaList;
List<String> RodzajList;
List<String> ProgramList;
List<String> UwagiList;
List<String> listaDni;
List<String> listaMiesiecy;
File plik;
public class podzialPlanu implements Serializable{
String DzienData;
String Godzina;
String Rodzaj;
String Program;
String Uwagi;
}
AdapterDlaPlanu nowyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewy);
nowyAdapter = new AdapterDlaPlanu();
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter(nowyAdapter);
}
public class AdapterDlaPlanu extends BaseAdapter implements Serializable{
List<podzialPlanu> podzialPlanuList = getData();
@Override
public int getCount() {
return podzialPlanuList.size();
}
@Override
public Object getItem(int position) {
return podzialPlanuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) AdapterPlanuProbOffline.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.planproby,parent,false);
}
TextView DzienDataTextView = (TextView) convertView.findViewById(R.id.dzienDataTextView);
TextView GodzinaTextView = (TextView) convertView.findViewById(R.id.godzinaTextView);
TextView RodzajZajecTextView = (TextView) convertView.findViewById(R.id.rodzajzajecTextView);
TextView ProgramTextView = (TextView) convertView.findViewById(R.id.programTextView);
TextView UwagiTextView = (TextView) convertView.findViewById(R.id.uwagiTextView);
/// here is a problem with getData() method
DzienDataTextView.setText(getData().get(position).DzienData);
GodzinaTextView.setText(getData().get(position).Godzina);
RodzajZajecTextView.setText(getData().get(position).Rodzaj);
ProgramTextView.setText(getData().get(position).Program);
UwagiTextView.setText(getData().get(position).Uwagi);
return convertView;
}
public podzialPlanu getPodzialPlanu(int position){
return podzialPlanuList.get(position);
}
}
public List<podzialPlanu> getData() {
List<podzialPlanu> podzialPlanuList2 = new ArrayList<podzialPlanu>();
List< List<String> > listaGlowna = new ArrayList<List<String>>();
for( int i = 0; i < 5; i++ ) {
listaGlowna.add(new ArrayList<String>());
}
DzienDataList = new ArrayList<String>();
GodzinaList = new ArrayList<String>();
RodzajList = new ArrayList<String>();
ProgramList = new ArrayList<String>();
UwagiList = new ArrayList<String>();
listaDni = new ArrayList<String>();
listaMiesiecy = new ArrayList<String>();
try {
FileInputStream file = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/" + "planProby.sav");
ObjectInputStream restore = new ObjectInputStream(file);
podzialPlanuList2 = (List<podzialPlanu>) restore.readObject();
restore.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return podzialPlanuList2;
}
Upvotes: 0
Views: 723
Reputation: 718718
Well, lets look at the exception message:
java.lang.ClassCastException:
pl.pawelfrydrych.CHAPS.AdapterPlanuProbOnline$podzialPlanu
cannot be cast to
pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline$podzialPlanu
Do you see the difference? Notice that there are two different podzialPlanu
classes.
Now if the SaveAsFile
method in your Question is the one that created the serialization, then it follows that the List<podzialPlanu>
argument type is actually referring to a:
pl.pawelfrydrych.CHAPS.AdapterPlanuProbOnline.podzialPlanu
rather than
pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline.podzialPlanu
Yes, so what should I do? If is there serialization, then I can't get this data in another class?
OK, so the real problem is not serialization. You would have the same problem if you skipped the serialize / deserialize step.
The fact is that those two nested classes are unrelated. You cannot cast between unrelated classes. So you either need to:
Assuming that the inner classes are public
, you shouldn't have "access" problems.
Upvotes: 1