1. Download our Official Android App: Forums for Android!

Apps String problem (sqlite-query)

Discussion in 'Android Development' started by EcDroid, Jun 13, 2011.

  1. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Hello to everyone,

    I
     

    Advertisement

  2. novox77

    novox77 Leeeroy Jennnkinnns!
    Rank:
    None
    Points:
    413
    Posts:
    3,965
    Joined:
    Jul 7, 2010

    Jul 7, 2010
    3,965
    3,257
    413
    the only thing I can think of is that the query1 String is null at the time that SQL is executed. Have you logged out the fully formed SQL string to verify that your query1 variable is initialized when the emulator force closes?
     
  3. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Hmm....the Toast displays the same string..
     
  4. novox77

    novox77 Leeeroy Jennnkinnns!
    Rank:
    None
    Points:
    413
    Posts:
    3,965
    Joined:
    Jul 7, 2010

    Jul 7, 2010
    3,965
    3,257
    413
    Ok, so you are using a toast to see what is in query1... and can I assume that query1 = "Test"?

    where in the code are you calling the toast? Could the value of query1 have changed by the time the SQL is called?

    Put your Toast.makeText(...) call right before
    Code (Text):
    1. String keyword = "name1 LIKE '%"+query1+"%'";
    Is it still the string you expect it to be?
     
  5. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Seems like the Toast will not be shown, when I put it right before this line?! It all seems to be very slow....but it seems, you`re right.

    I call ladeDaten(String query1) at the end of an onItemSelected (OnItemSelectedListener).
    query1 = selecteditem...it looks like the value (query1) of item is still null?! ...any ideas or workaround for that?
    As I said, I
     
  6. novox77

    novox77 Leeeroy Jennnkinnns!
    Rank:
    None
    Points:
    413
    Posts:
    3,965
    Joined:
    Jul 7, 2010

    Jul 7, 2010
    3,965
    3,257
    413
    post your code for the selected item listener... maybe we can figure out what's wrong there.

    I'm a Java developer for web applications (J2EE) but not really for Android, so I haven't really worked with Android event handlers a lot... But maybe an extra pair of eyes on your code is all we need here :)
     
  7. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    thanks :)
    It`s just this:

    public class MyOnItemSelectedListener1 implements OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent,
    View view, int pos, long id) {
    ladeDaten(parent.getItemAtPosition(pos).toString());
    }

    public void onNothingSelected(AdapterView<?> parent) {
    // Do nothing. }}
    }
    }


    This was the one with the toast:

    public
    class MyOnItemSelectedListener1 implements OnItemSelectedListener {
    publicvoid onItemSelected(AdapterView<?> parent,
    View view, int pos, long id) {
    Toast.makeText(parent.getContext(),
    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
    ladeDaten(parent.getItemAtPosition(pos).toString());
    }

    publicvoid onNothingSelected(AdapterView<?> parent) {
    // Do nothing. }}
    }
    }

     
  8. novox77

    novox77 Leeeroy Jennnkinnns!
    Rank:
    None
    Points:
    413
    Posts:
    3,965
    Joined:
    Jul 7, 2010

    Jul 7, 2010
    3,965
    3,257
    413
    I had to do some googling to figure out how the listener works. Seems to me you are missing a constructor for your listener class, where you would define the activity and adapter for your listener. See this example:

    SpinnerActivity.java | Android Developers

    Note the constructor:

    Code (Text):
    1.  
    2.         /**
    3.          *  Constructor
    4.          *  @param c - The activity that displays the Spinner.
    5.          *  @param ad - The Adapter view that
    6.          *    controls the Spinner.
    7.          *  Instantiate a new listener object.
    8.          */
    9.         public myOnItemSelectedListener(Activity c, ArrayAdapter<CharSequence> ad) {
    10.  
    11.           this.mLocalContext = c;
    12.           this.mLocalAdapter = ad;
    13.  
    14.         }
    15.  

    I don't see a constructor in your code block. Which means your listener is listening to nothing. That could explain the null :)
     
  9. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Hmm...constructor...thanks...I
     
  10. novox77

    novox77 Leeeroy Jennnkinnns!
    Rank:
    None
    Points:
    413
    Posts:
    3,965
    Joined:
    Jul 7, 2010

    Jul 7, 2010
    3,965
    3,257
    413
     
  11. novox77

    novox77 Leeeroy Jennnkinnns!
    Rank:
    None
    Points:
    413
    Posts:
    3,965
    Joined:
    Jul 7, 2010

    Jul 7, 2010
    3,965
    3,257
    413
    BTW, welcome to the forums :)

    I'm going to move your post from the Android Lounge to the Android Development area. Perhaps the people there, who surely are more versed than me, can help you get past your current issue.
     
  12. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Thanks, I will try to check, how to do this...

    My code comes from here (I just adapted it):
    Spinner | Android Developers ...but it still amost the same.

    It`s really strange, because I made a .setText(query1) right before the query (one line before) -> and query1 is displayed correctly in the TextView....Hmmm...
     
  13. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Another problem, but I think, that`s my very small knowledge:

    This one works:
    Cursor klassenCursor = mDatenbank.query(
    "Tabelle",
    new String[] {
    "feld1",
    "feld2"
    },
    null, null, null, null,null );


    this not:
    String keyword = null;
    Cursor klassenCursor = mDatenbank.query(
    "Tabelle",
    new String[] {
    "feld1",
    "feld2"
    },
    keyword, null, null, null,null );


    Anyone, who could tell me why?

    Thank you
     
  14. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Oh, my third post in a row...I have solved the first issue:

    onItemSelected will also be called at onCreate and that`s the problem...at least it seems, it`s okay now :)
    I just use a boolean to check, if onItemSelected is called for the first time...

    But I still don`t know, why I can`t set a string to null....
     
  15. novox77

    novox77 Leeeroy Jennnkinnns!
    Rank:
    None
    Points:
    413
    Posts:
    3,965
    Joined:
    Jul 7, 2010

    Jul 7, 2010
    3,965
    3,257
    413
    do a null check and convert it to an empty string. the null is causing the SQL query to be malformed.



    Code (Text):
    1.  
    2. if (query1 == null) query1 = "";
    3. String keyword = "name1 LIKE '%"+query1+"%'";
    4.  
    that should fix it I think.
     
  16. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    btw, just to clarify, you shouldn't *technically* need a constructor for listener objects. Though the code and examples above make use of them.

    Also @OP you should, when you get some time learn about binding your SQL queries

    Also, the reason you cant convert null to a string is that you cant point to null.

    To phrase it differently, you cant convert "nothing" into a string.
     
  17. EcDroid

    EcDroid Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Jun 13, 2011

    Jun 13, 2011
    9
    0
    5
    Thank you! :)
     

Share This Page

Loading...