简体   繁体   中英

Retrieve data from the database when Search Button is click in Android

I have editText's (Username, Firstname, Lastname and Email Address) in my activity for user registration. The user has the privilege to search if the username that he input is already existing or not, by clicking the Search Button . If the username is existing, all the information with regards to that username like the name of the user will be showed. However, if the user click the button if it is not existing, my app crashes and I am getting CursorIndexOutOfBoundsException error. How can I debug that one?

MainActivity.java

btn_Search.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        String searchableUser = txt_User.getText().toString();

        ConsUserRegistration consUserRegistration = db.searchUser(searchableUser);

        String searchUser = consUserRegistration.getUser().toString();
        String searchFirst = consUserRegistration.getFirstName().toString();
        String searchLast = consUserRegistration.getLastName().toString();
        String searchEmail = consUserRegistration.getEmail().toString();

        txt_User.setText(searchUser);
        txt_First.setText(searchFirst);
        txt_Last.setText(searchLast);
        txt_Email.setText(searchEmail);             
    }
});

DatabaseHandler.java

public ConsUserRegistration searchUser(String username){

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(Constants.TABLE_USER, new String[] {Constants.KEY_USER, Constants.KEY_FIRST, 
            Constants.KEY_LAST, Constants.KEY_EMAIL}, Constants.KEY_USER + " =? ", 
            new String[] { String.valueOf(username) }, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    ConsUserRegistration search = new ConsUserRegistration (cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3));

    return search;

}

so... what happens if your cursor IS null or has no elements? you're still trying to access it ...

Try something like...

if (cursor != null && cursor.moveToFirst())  {    

...

Try this:

MainActivity.java

btn_Search.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        String searchableUser = txt_User.getText().toString();

        ConsUserRegistration consUserRegistration = db.searchUser(searchableUser);
        if (consUserRegistration != null){

           String searchUser = consUserRegistration.getUser().toString();
           String searchFirst = consUserRegistration.getFirstName().toString();
           String searchLast = consUserRegistration.getLastName().toString();
           String searchEmail = consUserRegistration.getEmail().toString();

           txt_User.setText(searchUser);
           txt_First.setText(searchFirst);
           txt_Last.setText(searchLast);
           txt_Email.setText(searchEmail);
        }else{
         Toast.makeText(getApplicationContext(), "Username Not Found", Toast.LENGTH_LONG).show();
        }               
    }
});

DatabaseHandler.java

public ConsUserRegistration searchUser(String username){
    ConsUserRegistration search;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(Constants.TABLE_USER, new String[] {Constants.KEY_USER, Constants.KEY_FIRST, 
            Constants.KEY_LAST, Constants.KEY_EMAIL}, Constants.KEY_USER + " =? ", 
            new String[] { String.valueOf(username) }, null, null, null);
    if (cursor != null && cursor.moveToFirst()){
            search = new ConsUserRegistration (cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3));
        }else{
            search = null;
        }

    return search;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM