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);
 }
}
  • 1
    Possible duplicate of [Java, how to compare Strings with String Arrays](http://stackoverflow.com/questions/8936141/java-how-to-compare-strings-with-string-arrays) – Pradeep Simha Oct 20 '15 at 12:03
  • O(N) solution is to iterate the array and check if current item is equal to given string ... – Selvin Oct 20 '15 at 12:10

2 Answers2

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.

Androider
  • 3,833
  • 2
  • 14
  • 24
  • how can i get item info from String Array Resources ?? – ViVek ViVi Oct 22 '15 at 04:19
  • 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' . – ViVek ViVi Oct 22 '15 at 04:32
  • You can get String Array Resources as: String[] bob=getResources().getStringArray(R.array.bob); – Androider Oct 22 '15 at 10:36
  • that only i earlier !! : String[] drugs = getResources().getStringArray(R.array.Drugsinfo); – ViVek ViVi Oct 24 '15 at 13:37
  • still am having problem in FOR statement ...getting yellow mark on FOR statement - for loop replacable with foreach – ViVek ViVi Nov 05 '15 at 05:26
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);    
  }
}
  • 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.... – ViVek ViVi Oct 22 '15 at 04:21
  • Sorry, I forgot to add the "toString()" part. I've edited the code – Olson Yarzagaray Oct 22 '15 at 08:14
  • still am having problem in FOR statement ...getting yellow mark on FOR statement - for loop replacable with foreach. – ViVek ViVi Oct 24 '15 at 13:40