1

I have to create an app in android with a database.In that database I have a predefined list of products.

Now,the thing is that my ap has to offer to the user the posibility to introduce in that list some other products which are not in the list.

To this end, I've created an autocomplete text view in which I introduce a new product and I take the text fro autocomplete and I have to write it in the database

Now,my problem is that when I display the products that I've introduced in the database,the toast text that I use to display what I have in the database it shows me nothing next to "product......".

Now,that may be because when I try to get the text from the autocomplete I get nothing in return?

This is how I read from autocomplete:

mItem = (AutoCompleteTextView) findViewById(R.id.todo_edit_item);

String nou=mItem.getText().toString();

And then I compare nou(which is what I wrote in the autocomplete) with what I have predefnied in the list,so if it is a new product(which was not in the list already) the I add it in the database:

for(int i = 0; i < l; i++)

    {


       if (nou!=fruits[i])
                t=true;
        else t=false;

     }
    if (t==true)
    {
        db.insertTitle(nou);
        fruits=db.getAllfromDB("Fruits","fruit");
        l=l+1;
    }

Anyone any ideas of what I'm doing wrong in here cause I can't figure out.I'lll be here for further details.Thank u in advance:)

MByD
  • 135,866
  • 28
  • 264
  • 277
Gaby
  • 11
  • 2

1 Answers1

0

You compare strings using != instead of using !nou.equals(fruits[i]). also you compare to all elements in array each time, since you so t is always the value of the comparison to the last element in the array whether a match was found or not.

It should be written like that:

t = true;
for(int i = 0; i < l; i++)
{
   if (nou.equals(fruits[i]))
   {
            t=false;
            break;
   }
}
if (t==true)
{
    db.insertTitle(nou);
    fruits=db.getAllfromDB("Fruits","fruit");
    l=l+1;
}
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Yes,I understand but I always introduce something that is not in the list and I always get nothing displayed ....Is correct how I read from the autocomplete?Thx – Gaby Apr 15 '11 at 20:26
  • Well,I tried,but still nothing displayed...and I really thing that is because it can't read from autocomplete the product I introduce – Gaby Apr 15 '11 at 20:34
  • Add a print of nou every time before the loop, since it's probably returning the full text, you can split it using split() – MByD Apr 15 '11 at 20:37
  • it is only a product so it's something like "apples" no need to set split! – Gaby Apr 15 '11 at 20:39
  • Yes,it doesn't display nothing!!!!!!I put print just before the loop and asked to display nou...but it showd nothing. – Gaby Apr 15 '11 at 20:49
  • So my way of getting the text from autocomplete is wrong.Do u have any idea of how can I fix that?Thank u!!! – Gaby Apr 15 '11 at 20:50
  • see here: http://stackoverflow.com/questions/4819813/how-to-get-text-from-autocomplete-textview-android – MByD Apr 15 '11 at 20:53