android - After Getting object/array from json, how can i shuffle it (Randomize) -
i got json data using this
``
private void loadquestions() throws exception { try { inputstream questions = this.getbasecontext().getresources() .openrawresource(r.raw.questions); breader = new bufferedreader(new inputstreamreader(questions)); stringbuilder quesstring = new stringbuilder(); string ajsonline = null; while ((ajsonline = breader.readline()) != null) { quesstring.append(ajsonline); } log.d(this.getclass().tostring(), quesstring.tostring()); jsonobject quesobj = new jsonobject(quesstring.tostring()); queslist = quesobj.getjsonarray("questions"); log.d(this.getclass().getname(), "num questions " + queslist.length()); } catch (exception e){ } { try { breader.close(); } catch (exception e) { log.e("", e.getmessage().tostring(), e.getcause()); } } } public static jsonarray getqueslist() { return queslist; }
``
here json data.
``
{ "questions": [ { "question": "which animal carnivorous?", "correctanswer": 1, "answers": [ { "answer": "cow" }, { "answer": "lion" }, { "answer": "goat" }, { "answer": "elephant" } ] }, { "question": "humans need", "correctanswer": 0, "answers": [ { "answer": "oxygen" }, { "answer": "nitrogen" }, { "answer": "carbondioxide" }, { "answer": "hydrogen" } ] }, { "question": "choose amphibian ", "correctanswer": 0, "answers": [ { "answer": "frog" }, { "answer": "owl" }, { "answer": "goat" }, { "answer": "fox" } ] }, { "question": "---- part of earth`s atmosphere.", "correctanswer": 1, "answers": [ { "answer": "unisphere" }, { "answer": "troposphere" }, { "answer": "oxysphere" }, { "answer": "carbosphere" } ] }, ] }
after getting json data need randomize it. brother please, have tried nothing working in advance
after queslist = quesobj.getjsonarray("questions"); // right place shuffle peterola
,add randomize questions list:
list<jsonobject> questionslist = new arraylist<jsonobject>(queslist.length()); for(int i=0,size=queslist.length();i<size;++i){ try { questionslist.add(queslist.getjsonobject(i)); } catch (jsonexception e) { e.printstacktrace(); } } long seed = system.nanotime(); collections.shuffle(questionslist, new random(seed));
Comments
Post a Comment