Problems with Sqlite cursor.getString() in if statement - android

I'm having problems with an IF-statement and comparing strings.
I did check this out to know more about string comparing.
How do I compare strings in Java?
If I understood this right "cursor.getString(0)" will return a string. Anyway everthing in my Sqlite table is of the type "text" I guess. I can print with log.d the string "name" and that show the correct word. But when it gets to compare strings in the IF-statement it does not match anything. I have no problem with Sqlite or any other parts of the code.
My if-statement does not work
Here is my code:
String Food = "Food";
while(cursor.moveToNext()){
String name = cursor.getString(0);
Log.d("name", "name: " + name); //This sprints: name: Food
if(name.equals(Food)){
// ...do something
}
else if (Food.equalsIgnoreCase(name)){
// ...do something else
}
}
It will just continue to print the rest of the column but I want to "do something" if I find a name "Food".
Can someone help find out what is wrong with my IF-statement?

just a short thing, please don't name a variable with a capital at the beginning (looks like a class). However, perhaps one of the Strings contains a space or something like that. Use the method trim before you compare your strings:
String food = "Food";
while(cursor.moveToNext()){
String name = cursor.getString(0);
Log.d("name", "name: '" + name + "'"); //This sprints: name: 'Food' ?
if(name.trim().equals(food.trim())){
// ...do something
} else if (food.trim().equalsIgnoreCase(name.trim())) {
// ...do something else
}
}
If this doesn't solve your problem, you need probably post more code, if it solves your problem, please rate this answer.

Related

How to compare a String with a database table name?

I have a database table in my android app. I want to compare database table name with a particular string.
Here is my code
if(branchName == TABLE_NAME)
{
}
else
{
Toast.makeText(this,"table not available",Toast.LENGTH_LONG).show();
}
Here branchName is the string.TABLE_NAME is a static string and it is the name of the table.
Whenever I run this code, control goes to else part and shows the toast message.
Why? How can I fix that?
for comparing two string you must use equal or equalsIgnoreCase ... ...
if (branchName.equalsIgnoreCase(TABLE_NAME) ){
// do sth
}
else{
Toast.makeText(this,"table not available",Toast.LENGTH_LONG).show();
}
try this way.

How to compare a String with an Array's any item in android

Suppose, I have a String named dogsStatus.
String dogsStatus = "Dogs are running";
I have also an Array named dogsChoice.
ArrayList<String> dogsChoice = new ArrayList<>();
dogsChoice.add("barking");
dogsChoice.add("running");
dogsChoice.add("eating");
Now, I want to setup a SWITCH-CASE with the String dogsStatus .
So please see the code if it is ever possible or not:-
switch(dogsStatus){
case "Dogs are" + dogsChoice:
//Do something
}
If it is not possible then is there any way to compare a String with an array's any value? Not any specific one...
Try:
dogsChoice.contains("your compare word");
Try this...........
for(int=0; i<dogsChoice.size();i++){
switch(dogsStatus){
case "Dogs are" + dogsChoice[i]:
//Do something
}
}
Try this:
Iterate your ArrayList and get the values
for(int=0; i<dogsChoice.size();i++){
if(dogsChoice.get(i).equalsIgnoreCase("your string to be compared")){
// do your stuff here
}
}

How to check String in Array List from SQLite [duplicate]

This question already has answers here:
Comparing phone numbers in Android
(4 answers)
Closed 8 years ago.
in my android app i have an SQLite Database with phone number (here I changed the numbers with other inexistent for privacy)!
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
I created a List containing the numbers from database and printed on the screen to see if it works and it give me back correct the numbers!
List numeri = mySQLiteAdapter.getAllNumeri();
System.out.println("num tel: " + phone);
It give me back two numbers present in database --> [3401234567, 02123456]
Then i tryed to compare the incoming number of a call with this List with this metod:
String phone = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("Incoming number - " + phone);
boolean retval = numeri.contains(phone);
if (retval == true) {
System.out.println("PHONE is contained in the list");
}
else {
System.out.println("PHONE is not contained in the list");
}
I tryed to call with my number 3401234567!
The println give me back: Incoming number: +393401234567
and
The condition give me back that "PHONE is not contained in the list!"
I tryed also to change database numbers by adding the national prefix (+39) but it don t works!
Can anyone help me? Thanks
I think it isn t duplicate because in other question, compare two string, here i want compare one string with arraylist
Did you check the return type of
mySQLiteAdapter.getAllNumeri()
if its number, then the below statement might not give the desired output as you are trying to compare the numbers with the Strings.
boolean retval = numeri.contains(phone);

android database delete row by content

Hey Guys ive got a problem with my database.
iam displaying my database in a textview looking like:
hh:mm dd:MM:yyyy text
12:14 12.12.2014 awdawdawd
13:12 13:12:2015 awdaw awdw
onclick iam getting the text by:
StringBuilder ortsplit = new StringBuilder();
String item = ((TextView) view).getText().toString();
String[] itemsplit = item.split("\\s+");
String uhrsplit = itemsplit[0].toString();
String datumsplit = itemsplit[1].toString();
ortsplit.setLength(0);
for (int i = 2; i < itemsplit.length; i++) {
ortsplit.append(" " + itemsplit[i].toString());
}
String sortsplit = String.valueOf(ortsplit);
then iam opening my database:
datasource.open();
datasource.findedel(uhrsplit,datumsplit,sortsplit);
datasource.close();
my datasource.findedel:
public void findedel(String pZeit, String pDatum, String pOrt) {
database.delete("TABELLE", "UHRZEIT="+Zeit +"AND DATUM="+Datum+"AND ORT="+Ort,null);
}
ive got no "id" displayed in the rows, earlier it looked like:
1 hh:mm dd:MM:yyyy text
2 12:14 12.12.2014 awdawdawd
3 13:12 13:12:2015 awdaw awdw
and ive just took the "id" and searched my entries for that id = id and deleted the row, but since i deleted the first row i want to search the row by the content.
any1 got a solution for my problem?
You have multiple errors and also you are prone to SQL injection.
You must use prepared statements or you must add quotes to your strings and escaping the quotes the string has, for example, in your code:
database.delete("TABELLE", "UHRZEIT="+Zeit +"AND DATUM="+Datum+"AND ORT="+Ort,null);
this: DATUM="+Datum+"AND is bad coded, there is not space between Datum and AND so, if datum is equal to test, then you string will be like this: DATUM=testAND. That will return syntax errors in mysql, and also string must be quoted like this: DATUM='test' AND.
The main problem of quoting this way is that if Datum has quotes by itself, you will have errors too. For example, if Datum equals to te'st then your string is going to be like this: DATUM='te'st' AND. As you see, you will have 3 quotes and then will return syntax error.
You must read and understand this before going further, because you will end up with a really messy code plenty of errors and vulnerabilities: http://wangling.me/2009/08/whats-good-about-selectionargs-in-sqlite-queries.html
Good luck ;)
And also, in Java all variable names must start in lowercase (Instead of String Datum use String datum)

need to retrieve data from the database

I have a very long string in the database that needs to be retrieved into a swipe view.
But,the problem is that the string comprises of set of "\n\n"
Whenever it is separated with this expression i need to put it in another slide,i mean i am using SWIPE view here..
if(tablecolumn==\\n\\n)
{
code to break it to parts
}
Is this how i should be doing it?
If i am wrong,how to break this string to different parts and enable it into SWIPE VIEW in to different swipe view?
You can simply break your string comprising of a special character like this :-
String str ="mynameisjhon.yournameisdash.bla";
, here you have a string concatenated with " . " (period character)
to break this string do this :-
StringTokenizer st = new StringTokenizer(str, "."); //break the string whenever "." occurs
String temp =st.nextToken(); // it will have "my name is jhon" break
String temp2 = st.nextToken();// it will have "your name is dash"
String temp3 = st.nextToken();//it will have "bla"
now your string is breaked into parts!
Anything else?
Load the whole string into your ViewAdapter and seperate it via substring
or load the string in your Activity/Fragment seperate it via substring, put the strings in an ArrayList, an initiate your ViewAdapter with the ArrayList as data source
either way use substring

Categories

Resources