0

I am creating a Word-Game-Like application. I had an idea of making an arraylist on strings.xml for the Words and Questions. How would i check(in .java file) if the entered word is in the array list?

And then if the word is in there, it will show that it is correct. (Guess i have to use Toast if im not mistaken)

UPDATE: I now have an array.xml, and here's my current class file

ArrayAct.java

public class ArrayAct extends Activity{
Button mButton;
EditText mEdit;
String [] mArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.arrayact);


    mArray = getResources().getStringArray(R.array.NameArrays);

    final Button doneBtn = (Button)findViewById(R.id.doneBtn);
    final EditText text = (EditText)findViewById(R.id.txtFld);

    doneBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (mArray.equals(text.getText().toString())) 
                Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
            else 
                Toast.makeText(getApplicationContext(), "Incorrect.Please try again.", Toast.LENGTH_SHORT).show();

        }
    });

....

The result always show incorrect even if i input a word that is the same in the array. What might i be doing wrong in this case?

Kaitlin Reyes
  • 71
  • 1
  • 8

1 Answers1

0

Well... you could divide your problem into sub problems. Then search the solutions on stackoverflow. Here is the steps

Step 1: Get array from xml file:

 //array in array.xml
 <resources>  
 <array name="testArray">  
    <item>first</item>  
    <item>second</item>  
    <item>third</item>  
    <item>fourth</item>  
    <item>fifth</item>  
 </array>
 </resources> 

String [] mArray = getResources().getStringArray(R.array.testArray);

Step 2: Get the text from EditText' usinggetText()` on Button click(if you have one). Check Get Value of a Edit Text field

Step 3: Compare both strings using .equals() method. Show toast accordingly. How to display Toast in Android?

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • @KaitlinReyes Issue is `mArray.equals(text.getText().toString())`. You are comparing the word directly to the array. Run a loop through the elements of the array and check if the work is equal to any of them. – Shobhit Puri Aug 25 '14 at 22:13
  • Did this **if (Arrays.asList(mArray).contains(text.getText().toString()))** and it worked. Thanks again – Kaitlin Reyes Aug 26 '14 at 04:15