getParcelableArrayList returns a Parcelable but i need it to return my object. (java android) -
i got error below:
error:(133, 15) error: method setmonkeybuisness in class quotebank cannot applied given types; required: arraylist<quotequestion> found: arraylist<parcelable> reason: actual argument arraylist<parcelable> cannot converted arraylist<quotequestion> method invocation conversion
both quotequestion
, quotebank
implement parcelable
, methods. cannot type cast parcelable either.
am using parcelable
array list correctly?
here part of code quotebank
:
public class quotebank implements parcelable{ public static final string array_list_key = "arraylistkey"; private arraylist<quotequestion> monkeybuisness; public quotebank(){ } @override public void writetoparcel(parcel dest, int flags) { bundle bundle = new bundle(); bundle.putparcelablearraylist(array_list_key, monkeybuisness); dest.writebundle(bundle); } public static final parcelable.creator<quotebank> creator = new creator<quotebank>() { @override public quotebank createfromparcel(parcel source) { bundle bundle = source.readbundle(); quotebank qb = new quotebank(); qb.setmonkeybuisness(bundle.getparcelablearraylist(array_list_key)); return qb; } public void setmonkeybuisness(arraylist<quotequestion> monkeybuisness) { this.monkeybuisness = monkeybuisness; }
here quotequestion
code:
public class quotequestion implements parcelable{ public static final string quote_type = "quotetype"; public static final string quote_number = "quotenumber"; public static final string quote_array = "quotearray"; public static final string speaker_array = "speakerarray"; public static final string answer_index_array = "answerindexarray"; public static final string answer_choice_array = "answerchoicearray"; public static final string context_key = "contextkey"; public static final string chosen_answer = "chosenanswer"; public static final string word_split = "wordsplit"; private int quotenumber; private string quotetype; private arraylist<string> quote; private arraylist<string> speaker; private arraylist<integer> answerindex; private arraylist<string> answerchoice; private string context; private string chosenanswer; @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { bundle bundle = new bundle(); // insert key value pairs bundle bundle.putint(quote_number, quotenumber); bundle.putstring(quote_type, quotetype); bundle.putstringarraylist(quote_array, quote); bundle.putstringarraylist(speaker_array, speaker); bundle.putintegerarraylist(answer_index_array, answerindex); bundle.putstringarraylist(answer_choice_array, answerchoice); bundle.putstring(context_key, context); bundle.putstring(chosen_answer, chosenanswer); bundle.putstringarraylist(word_split, wordsplittypea); // write key value pairs parcel dest.writebundle(bundle); } public static final parcelable.creator<quotequestion> creator = new creator<quotequestion>() { @override public quotequestion createfromparcel(parcel source) { // read bundle containing key value pairs parcel bundle bundle = source.readbundle(); quotequestion quotequestion = new quotequestion(); quotequestion.setquotenumber(bundle.getint(quote_number)); quotequestion.setquotetype(bundle.getstring(quote_type)); quotequestion.setquote(bundle.getstringarraylist(quote_array)); quotequestion.setspeaker(bundle.getstringarraylist(speaker_array)); quotequestion.setanswerindex(bundle.getintegerarraylist(answer_index_array)); quotequestion.setanswerchoice(bundle.getstringarraylist(answer_choice_array)); quotequestion.setcontext(bundle.getstring(context_key)); quotequestion.setchosenanswer(bundle.getstring(chosen_answer)); quotequestion.setwordsplittypea(bundle.getstringarraylist(word_split)); return quotequestion; } @override public quotequestion[] newarray(int size) { return new quotequestion[size]; } };
also have second question while here - seems big multi ui apps have classes implement parcelable? way data around app? best practice?
split statement two, using variable hold properly-typed arraylist
, this:
arraylist<quotequestion> qq = bundle.getparcelablearraylist(array_list_key); qb.setmonkeybuisness(qq);
why works, whereas casting not? have no idea. if knows, please replace paragraph!
as second question, implementing parcelable
on place: temporary-by-design nature of activities , heavy usage of intents can lead requiring parcelable
in many places. app design patterns can mitigate problem. example, following mvc techniques, application data can live within model, accessible via custom class derived application
. allows activities avoid saving & restoring bundled data, views of model, persists across device rotations etc. bigger topic of course, many different approaches, sparks ideas.
Comments
Post a Comment