Saving data with SQLite and adding it to a ListView



➤Android SQLite for BEGINNERS Course 2019:

Source Code:

➤My Keyboard:

➤My Microphone:

➤Check out our website:

38 comments

  • for people who are getting a CRASH !!, you need to put empty space between the word ( FROM ) and the star ( * ) in the database last line . Mr Mitch please make a comment regarding this point . thank .

  • how is this
    public Collection<User> readAllInfor() {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor result = db.query(UserProfile.Users.TABLE_USER_INFO, null, null, null, null, null, null);

    if(result.getCount() == 0) {
    result.close();
    db.close();
    return null;
    }

    User user;
    Collection<User> usersList = new ArrayList<>();
    while(result.moveToNext()) {
    user = new User(result.getInt(result.getColumnIndexOrThrow(UserProfile.Users._ID)),
    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)),
    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH)),
    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)),
    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_GENDER)));
    usersList.add(user);
    }

    result.close();
    db.close();
    return usersList;
    }

    /**
    * Check User Class for more information.
    */
    public User readAllInfor(String username) {
    SQLiteDatabase db = this.getReadableDatabase();

    String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "=?";

    Cursor result = db.query(UserProfile.Users.TABLE_USER_INFO, null, whereClause, new String[] {username}, null, null, null);

    if(result.getCount() == 0) {
    db.close();
    result.close();
    return null;
    }

    User user = null;
    while(result.moveToNext()) {
    if(result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)).equals(username)) {
    user = new User(result.getInt(result.getColumnIndexOrThrow(UserProfile.Users._ID)),
    username,
    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH)),
    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)),
    result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_GENDER)));
    }
    }

    db.close();
    result.close();
    return user;
    }
    I added this in DPHelper file

  • For those getting a null pointer exception error…

    You need to create a new object of the DatabaseHelper Class in MainActivity.java

    Insert this line

    myDB = new DatabaseHelper(this);

    below the line

    btnView = (Button) findViewById (R.id.btnView);

    You can see it at 15.02 time point in the video. Somehow Mitch forgot to write about it.

    All in all it is a very very good tutorial but yes you need to have java knowledge and debugging skills to workaround such missed points.

    Ravi.

  • my data is not being inserted… whever i add data it says something went wrong, means the if else condition for the result == -1 have some error…… please help me to solve as soon as possible.

  • thanks for the good tutorial, but how if i want to click that listview and go to another activity?
    im still confused how to throw the data from the listview to the new activity. thanks

  • I'm getting the following catlog error, new to android don't know what is the issue.I tried debugging but no use.
    Please help me out!

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.administrator.sample2, PID: 9816
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.sample2/com.example.administrator.sample2.Display}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
    at android.app.ActivityThread.access$900(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.example.administrator.sample2.Display.onCreate(Display.java:29)
    at android.app.Activity.performCreate(Activity.java:5451)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
    at android.app.ActivityThread.access$900(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at dalvik.system.NativeStart.main(Native Method) 
    I/Process: Sending signal. PID: 9816 SIG: 9
    Application terminated.

  • Thanks man. 😀

    This is the tutorial I was searching for. I need another help with connecting two tables. When I touch table one's any data it will show another related table's data. I would appreciate your reply.

  • YOUR COMMENT