0

I'm beginner for Android, im having stringArray resource on string.xml file.. i need check whether the item name is correct then i hav to do something.. im entering the correct item name but it goes to else part.. may be the problem is in if(itemname=stringArray).. plz give me the solution ..

String[]  drugs = getResources().getStringArray(R.array.Drugsinfo);

if(editext1.getText().equals(drugs))        

{

Toast.makeText(getApplicationContext(), "some usefull info for you buddy ...",    Toast.LENGTH_SHORT).show();

Intent myIntent = new Intent(MainActivity.this, Descriptionjava.class);
                startActivity(myIntent);
}

else 

{
 Toast.makeText(getApplicationContext(), "Wrong pill name !! check out the list",Toast.LENGTH_LONG).show();
 bt1.setEnabled(true);
 }
}
2
  • 1
    Possible duplicate of Java, how to compare Strings with String Arrays Commented Oct 20, 2015 at 12:03
  • O(N) solution is to iterate the array and check if current item is equal to given string ... Commented Oct 20, 2015 at 12:10

2 Answers 2

2

Try this:

        String[] bob = { "this", "is", "a", "really", "silly", "list" };

        if (Arrays.asList(bob).contains("silly")) {
            // true
        }

"silly" is text of your EditText.

For getting text from edit text and compare according to your comment use some like:

if(yourEditText.getText().toString().trim().equalsIgnoreCase("silly"))
{
//do your work here
}

Thanks.

Sign up to request clarification or add additional context in comments.

5 Comments

how can i get item info from String Array Resources ??
im having all item names in StringArray resource.. either we can use String[] bob = { "this", "is", "a", "really", "silly", "list" }; like this... if am entering one item name in edittext box i should get the exact item name .. String[] bob = { "this", "is", "a", "really", "silly", "list" }; if(et1.getText().toString().equals(Arrays.asList(bob).contains("silly"))) { // true } equals() between objects of inconvertible types 'string' and 'editable' .
You can get String Array Resources as: String[] bob=getResources().getStringArray(R.array.bob);
that only i earlier !! : String[] drugs = getResources().getStringArray(R.array.Drugsinfo);
still am having problem in FOR statement ...getting yellow mark on FOR statement - for loop replacable with foreach
0

I haven't test it but you could try this.

String[]  drugs = getResources().getStringArray(R.array.Drugsinfo);

for(int index = 0; index < drugs.length;index++) {
   if(editext1.getText().toString().equals(drugs[index])) {
      Toast.makeText(getApplicationContext(), "some usefull info for you  buddy        ...",Toast.LENGTH_SHORT).show();

    Intent myIntent = new Intent(MainActivity.this, Descriptionjava.class);
    startActivity(myIntent);
} else {
   Toast.makeText(getApplicationContext(), "Wrong pill name !! check out the       list",Toast.LENGTH_LONG).show();
   bt1.setEnabled(true);    
  }
}

3 Comments

I tried this !! getting yellow mark on equals() and FOR .....(i) equals() between objects of inconvertible types 'string' and 'editable' .. (ii) for loop replacable with foreach....
Sorry, I forgot to add the "toString()" part. I've edited the code
still am having problem in FOR statement ...getting yellow mark on FOR statement - for loop replacable with foreach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.