Pages

GRE5000

After finishing some GRE quizzes that I downloaded a while ago, I wanted to have those with me so that I could review them again. Hence, this app. The app randomly chooses 1 of 50 quizzes. From that quiz, it chooses 1 of 100 questions. The app uses random number generators, Jsoup parser, AsyncTask and javascript.



 Code:

onCreate
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  webViewCanvas = (WebView) findViewById(R.id.webView1);
  webViewCanvas.getSettings().setJavaScriptEnabled(true);
  button = (Button) findViewById(R.id.button1);
  loadingScreen = (ImageView) findViewById(R.id.imageView1);
  AsyncTaskRunner shuffleForNewWord = new AsyncTaskRunner();
  shuffleForNewWord.execute();
 }


AsyncTask

 private class AsyncTaskRunner extends AsyncTask<String, String, String> {

  private String word, answer, options;
  private String result;

  @Override
  protected String doInBackground(String... params) {
   String file = "words_college_gre_words-quiz-" + randomQuiz()+ ".htm";
   InputStream greFiles = null;

   try {
    greFiles = getAssets().open(file);
    Document doc = Jsoup.parse(greFiles, "UTF-8","http://example.com/");
    Element selectedQuizQuestion = (doc.select("tr").get(randomQuestion()));
    String raw = selectedQuizQuestion.toString();
    
    //...............Editing raw to get result.......................//
    //.......Adding html, javascript, and pretty printing............//
    result = //getting result from above described process 
    //sending word to publishProgress to print it as Button label
    //doInBackground should have nothing to do with controlling UI/main thread
    publishProgress(word);
    
   } catch (IOException e) {
    e.printStackTrace();
   }

   finally {
    if (greFiles != null)
     try {
      greFiles.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
   }
   return result;
  }

  @Override
  protected void onPostExecute(String result) {
   //changing to transparent image once the result is ready
   loadingScreen.setImageResource(R.drawable.transparent);
   webViewCanvas.loadData(result, "text/html", "UTF-8");
  }

  @Override
  protected void onPreExecute() {
   button.setText("SHUFFLING");
   //making the webview color consistent 
   webViewCanvas.loadData("<html><body bgcolor=\"#8cdfff\"></html></body>","text/html", "UTF-8");
   loadingScreen.setImageResource(R.drawable.nottransparent);
  }

  @Override
  protected void onProgressUpdate(String... text) {
   button.setText(word);
  }

 }

Screenshots:





Ayush Subedi

Coffee Connoisseur