Displaying ALL data from sqlite database into listview in tabbed activity

0 votes

I've been struggling with this problem for a few weeks as a novice android developer, and it's getting very exhausting.

I've read through every lesson and question and answer I could find, but I still don't understand how to get Android Studio to just copy the contents of my SQLite database and paste them into a listview. I had assumed there would be some form of android:displayallfrom("myDB") call in the XML files to just show everything in a database, however it seems to be a much more difficult.

Basically, what I want to do is display ALL data from my database (Dogs.db) into my listview (list_dogs) in the first tab of my tab view (Tab1).

Here is my code:

Tab1.java

package com.example.major.awoo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Tab1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab1, container, false);
        return rootView;
    }
}

tab1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.major.awoo.MainActivity">

    <ListView
        android:id="@+id/list_dogs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:listSelector=""/>

</RelativeLayout>

DatabaseHelper.java

package com.example.major.awoo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Dogs.db";
    public static final String TABLE_NAME = "dogs_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "AGE";
    public static final String COL_4 = "WEIGHT";
    public static final String COL_5 = "BREED";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String name,String age,String weight,String breed) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,age);
        contentValues.put(COL_4,weight);
        contentValues.put(COL_5,breed);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if(result == -1)
            return false;
        else
            return true;
    }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }

    public boolean updateData(String id,String name,String age,String weight,String breed) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,id);
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,age);
        contentValues.put(COL_4,weight);
        contentValues.put(COL_5,breed);
        db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
        return true;
    }

    public Integer deleteData (String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "ID = ?",new String[] {id});

    }

    public Cursor getListContents(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
        return data;
}

//method to display data

    public Cursor displayData;
    {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery(" SELECT  * FROM " + TABLE_NAME, null);
        return res;    
    }   

}
Aug 20, 2022 in Database by Kithuzzz
• 38,000 points
3,183 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Database

0 votes
0 answers
0 votes
0 answers

Display data from SQL database into php/ html table.

I want to display one of my ...READ MORE

Aug 27, 2022 in Database by Kithuzzz
• 38,000 points
875 views
0 votes
0 answers

Backup a single table with its data from a database in sql server 2008

I want to use a script to ...READ MORE

Aug 29, 2022 in Database by Kithuzzz
• 38,000 points
496 views
0 votes
1 answer

How can I check my all existing table in MySQL database?

Hi Pratik, I understand your problem regarding this ...READ MORE

answered Jul 1, 2019 in Database by sampriti
• 1,120 points
1,441 views
0 votes
1 answer

How to load data of .csv file in MySQL Database Table?

At first, put the dataset in the ...READ MORE

answered Jul 5, 2019 in Database by Reshma
1,462 views
0 votes
2 answers

How to send data to my database from html and css Contact Us Form?

Hello @Sign, It is simple to create contact ...READ MORE

answered Aug 4, 2020 in Database by Niroj
• 82,840 points
36,040 views
0 votes
0 answers

How to retrieve data from sqlite database in android and display it in TextView

I'm studying Android. I'm having trouble solving ...READ MORE

Aug 11, 2022 in Database by Kithuzzz
• 38,000 points
4,840 views
0 votes
0 answers
0 votes
0 answers

SQLite in Android How to update a specific row

For a time now, I've been attempting ...READ MORE

Aug 11, 2022 in Database by Kithuzzz
• 38,000 points
1,735 views
0 votes
0 answers

How to store image in SQLite database

I want to upload an image from ...READ MORE

Aug 22, 2022 in Database by Kithuzzz
• 38,000 points
3,071 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP