Back to project page Memory-Training-Android-Application.
The source code is released under:
MIT License
If you think the Android project Memory-Training-Android-Application listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package markqdavenport.net.memorizingexercise; /*from w w w . j a va 2s . co m*/ import android.app.Activity; import android.content.Context; import android.content.res.AssetManager; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Random; /** * Created by mark on 12/31/14. */ public class WordExercise extends Activity{ private ArrayList<String> dictionary; private int wordLength; //Set elsewhere private String currentMemorizingList; private void createDictionary(){ dictionary = new ArrayList<String>(); BufferedReader dict = null; //Holds the dictionary.txt file AssetManager am = this.getAssets(); try { //dictionary.txt.txt should be in the assets folder. Context context = getApplicationContext(); CharSequence text = "Before creating dictionary.txt."; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); dict = new BufferedReader(new InputStreamReader(am.open("raw/dictionary.txt"))); String word; while((word = dict.readLine()) != null){ dictionary.add(word); } } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { dict.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //Precondition: the dictionary.txt has been created. private String getRandomWord(){ if(dictionary == null) { dictionary.add("zinga"); } return dictionary.get((int)(Math.random() * dictionary.size())); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wordmemorizing); } public void genWords(View view) { EditText answerBox = (EditText)findViewById(R.id.answerBox); try { EditText numberOfWordsInput = (EditText)findViewById(R.id.numberOfWords); Resources res = getResources(); InputStream in_s = res.openRawResource(R.raw.dictionary); byte[] b = new byte[in_s.available()]; in_s.read(b); String dictionaryInTotal = new String(b); String[] dictionaryInTotalArray = dictionaryInTotal.split(System.getProperty("line.separator")); Random random = new Random(); Integer numberOfWords = Integer.parseInt(numberOfWordsInput.getText().toString()); String[] randomWords = new String[numberOfWords]; for(int i = 0; i<numberOfWords; i++){ int randomIndex = random.nextInt(dictionaryInTotalArray.length); randomWords[i] = dictionaryInTotalArray[randomIndex]; } String wordList = stringArrayToString(randomWords); Integer inputNumber = Integer.parseInt(String.valueOf(numberOfWordsInput.getText())); answerBox.setText(wordList); currentMemorizingList = wordList; } catch (Exception e) { e.printStackTrace(); } } public void Increment(View view) { EditText numberOfWordsInput = (EditText)findViewById(R.id.numberOfWords); Integer numberOfWords = Integer.parseInt(numberOfWordsInput.getText().toString()); numberOfWords++; numberOfWordsInput.setText(numberOfWords.toString()); } public void Decrement(View view) { EditText numberOfWordsInput = (EditText)findViewById(R.id.numberOfWords); Integer numberOfWords = Integer.parseInt(numberOfWordsInput.getText().toString()); numberOfWords--; numberOfWordsInput.setText(numberOfWords.toString()); } public void showWords(View view) { EditText answerBox = (EditText)findViewById(R.id.answerBox); answerBox.setText(currentMemorizingList); } public void hideWords(View view) { EditText answerBox = (EditText)findViewById(R.id.answerBox); answerBox.setText(" "); } public static String stringArrayToString(String[] incomingStringArray) { String temp = ""; for(int p = 0; p<incomingStringArray.length;p++){ temp += " " + incomingStringArray[p] ; } return temp; } }