0

I'm trying to hold a set of ID's in an ArrayList I then want to compare the current value with the contents of said Arraylist, but it doesn't seem to work for me.

Both my methods are not working, in the code snippets c.getString(0) is pulling the Numeric ID column from a SQLite Database, and allProgramIds contains the same ID.

method 1. straight Comparison (this doesn't match, just ignores)

                for(int i=0;i<allProgramIds.size();i++) {
                      Log.w(TAG, "Looping - looking for:" + c.getString(0) + ", Holding:"+allProgramIds);

                        if( allProgramIds.get(i) ==  c.getString(0)) {
                            exist=true;
                            Log.w(TAG, "Found one:" + c.getString(0));
                            break;
                        }

                }

method 2. Casting to Int before Comparison (this causes crash)

                for(int i=0;i<allProgramIds.size();i++) {
                      Log.w(TAG, "Looping - looking for:" + c.getString(0) + ", Holding:"+allProgramIds);

                        if( Integer.toString((Integer) allProgramIds.get(i)) ==  c.getString(0)) {
                            exist=true;
                            Log.w(TAG, "Found one:" + c.getString(0));
                            break;
                        }

                }

here is a log output

 Log.w(TAG, "Looping - looking for:" + c.getString(0) + ", Holding:"+allProgramIds);

04-04 10:43:42.805: W/MainProgram(2045): Looping - looking for:73, Holding:[68, 69, 70, 71, 72, 73, 74, 75, 76, 68, 69, 70, 71, 72]

thanks

1 Answer 1

4

Use equals for String compares, not ==.

if(allProgramIds.get(i).equals(c.getString(0)))
// ...

== on objects checks for reference equality, not content equality (i.e. it compares the pointers to the memory location where the objects are stored).

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

1 Comment

ahha! awesome. I used that the other day too, and then totally forgot about it. thank you very muchski budski.

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.