0

Here is my Cursor code, what I am confused about is how do I go on checking each value and make my boolean valued x true or false based on the computation:

  private boolean fillData() {
       Cursor c = DBHelper.fetchAllIDs();
       // List<String> idList = new ArrayList<String>();
       if (c.moveToFirst()) {
        do {
            String X = (c.getString(c.getColumnIndexOrThrow("IDno")));
            Toast.makeText(getApplicationContext(), "" +X, Toast.LENGTH_LONG).show();
            if (Idno.getText().toString().equals(X));
            {
                Toast.makeText(getApplicationContext(),   ""+Idno.getText().toString(), Toast.LENGTH_LONG).show();
                x=true;
            }
        } while (c.moveToNext());


       }
      return x;
   }
Heretic Monk
  • 397
  • 1
  • 5
  • 19
  • 1. I don't quite follow what you're trying to do here. 2. Don't use == for String comparison: http://stackoverflow.com/questions/767372/java-string-equals-versus – Jesse Jashinsky Oct 05 '12 at 14:58
  • I want to compare each element I fetch from my database through this code with a value supplied by the user and if they match I want x to be true, what happens here is that even if a user supplies a different value than fetched my x still becomes true. – Heretic Monk Oct 06 '12 at 04:03

1 Answers1

0
  1. set x to false before looping on the cursor.

    private boolean fillData() {
        boolean x = false;
    
        // ...
    
    }
    
  2. replace the following code:

    if (Idno.getText().toString().equals(X));
    {
        Toast.makeText(getApplicationContext(),   ""+Idno.getText().toString(), Toast.LENGTH_LONG).show();
        x=true;
    }
    

    with this:

    if (Idno.getText().toString().equals(X))
    {
        Toast.makeText(getApplicationContext(),   ""+Idno.getText().toString(), Toast.LENGTH_LONG).show();
        x=true;
        break;
    }
    

    notice:
    i removed the ; from the end of the if statement, and added a break statement at the end of the if block.

kdehairy
  • 2,630
  • 22
  • 27