This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Android and the fly likes Android onClick on executing certain functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Android onClick on executing certain functions" Watch "Android onClick on executing certain functions" New topic

Android onClick on executing certain functions

Sebastian Zaba

Joined: Nov 30, 2010
Posts: 5
First I wanted to thank all the devoted programmers to this site it has been a large help to me. Now lets post something :) I spend 2 days on this now and its driving me crazy I am new to Android development and only somewhat comfortable with JAVA itself. Here is my problem I am writing a quick Android app to take a String permute it and check it against a dictionary but the onClick will only invoke and display the permute and not any of my other functions I tested the functions on a separate Java only tester class and the methods work just fine. here is the relevant Code:

public class HackaWordActivity extends Activity implements OnClickListener {
TextView enterLetters;
TextView possibleWords;
Button button1;
String inputString;
char[] chars;
ListView listView;

static ArrayAdapter<String> adapter; //to fill matches and display

/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {

enterLetters = (TextView) this.findViewById(;
listView = (ListView) findViewById(;

button1 = (Button) this.findViewById(;
//get user input
public String getString() {

inputString = (enterLetters.getText().toString());
return inputString;


public void onClick(View v) {

// adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,,Permutation.permute("", getString())); //this works and displays the results in the desired listView.

try {
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,,Permutation.compared(getString()));
//this does not work
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block

The Permutation Class:

//Permutation is a separate class with contains the methods to be implemented in HackaWordActivity
public class Permutation {

static ArrayList<String> wordArray = new ArrayList<String>();
public static String word;
public static String dictionaryCheck; //temporary holding place for dictionary words to compare
public static ArrayList<String> matches = new ArrayList<String>();

//recursive method that permutes chars in a given String. It takes a blank string, and a String
//It returns an ArrayList with the permuted chars as Strings
public static ArrayList<String> permute(String beginningString, String endingString) {

if (endingString.length() <= 1) {

word = (beginningString + endingString);

for (int i = 0; i < endingString.length(); i++) {
try {
String newString = endingString.substring(0, i) + endingString.substring(i + 1);
permute(beginningString + endingString.charAt(i), newString);
} catch (StringIndexOutOfBoundsException exception) {

return wordArray;


//Method to check if permuted Strings match words in a hard coded dictionary
public static ArrayList<String> checkDictionary (ArrayList<String> permutedArrayList) throws FileNotFoundException{

Scanner s = new Scanner(new File("dictionary1.txt")); //Creates scanner and reads file that must be in the Project folder
while (s.hasNext()){ //scan the document and set each word as String dictionaryCheck

dictionaryCheck =;

//check the provided arrayList to the dictionary Word saved in dictionaryCheck
for (int word = 0; word<permutedArrayList.size(); word++){
if (permutedArrayList.get(word).equals(dictionaryCheck)){

return matches;

//method that simplifies use my combining permuting and matching
public static ArrayList<String> compared (String input)throws FileNotFoundException{


return matches;

Sebastian Zaba

Joined: Nov 30, 2010
Posts: 5
OK I think i got it. But now:

Its not able to find the hard coded dictionary file how do i enter it so android can find it after installing the apk?

Scanner s = new Scanner(new File("dictionary.txt"));

I am having trouble using getResources() if it is the correct one to use.
I agree. Here's the link:
subject: Android onClick on executing certain functions
jQuery in Action, 3rd edition