开发者

can't get the text from an autocomplete

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:)


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜