I've managed to convert an arraylist of some class (in my case it's a class which extends ArrayAdapter<Question>) to an ArrayList<String>. I've passed it through an android intent bundle and received through the getStringArrayList method. Now I need to convert it back to the ArrayList<Question> to set my adapter up
Converting from ArrayList<Question> to ArrayList<String>
questionlists = task.execute(myitem).get();
for (Question object : questionlists)
{
strings.add(object != null ? object.toString() : null);
}
simpleArray2 = new String[strings.size()];
strings.toArray(simpleArray2);
Converting back to ArrayList<Question>
List<Question> variable;
lists = getArguments().getStringArrayList("Question");
variable = (List<Question>)(List<?>) lists;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_world, container, false);
ListView lv = (ListView) view.findViewById(R.id.list_sessions);
final QuestionAdapter adapter = new QuestionAdapter(getActivity(), variable);
lv.setAdapter(adapter);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(
//inflater.getContext(), android.R.layout.simple_list_item_1,
//lists);
// lv.setAdapter(adapter);
return view;
}
I've added a try/catch my adapter and I get the error:
java.lang.String cannot be cast to java.com.example.Question.
What am I doing wrong? Is it because I haven't converted back to a proper ArrayList first?
variable = (List<Question>)(List<?>) lists;what the. That won't work.List<Question>is not the same asList<String>becauseQuestionis not the same asString. You should make theQuestionclassParcelableusing parcelabler.com and send an array of Parcelable instead of String.