3 Replies - 623 Views - Last Post: 28 May 2017 - 08:53 AM

#1 Soumikbhat  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 01-September 13

Display data from Sqlite in ListView

Posted 15 March 2017 - 01:23 AM

I'm trying to display the data stored in a Sqlite database in a ListView.

This is my code so far

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

public class viewall extends AppCompatActivity {

    private SQLiteDatabase db;
    private Cursor c;
    private static final String SELECT_SQL = "SELECT * FROM task";
    ListView l;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewall);
        openDatabase();
        l=(ListView)findViewById(R.id.lv); //lv is the id of the ListView
        c = db.rawQuery(SELECT_SQL, null);
        c.moveToFirst();
        showRecords();
    }
    protected void showRecords() {
        do {
            String id = c.getString(0);
            String j = c.getString(1);
            String s = c.getString(2);
            String t = c.getString(3);
        } while (c.moveToNext());
        
    }
    protected void openDatabase(){
        db=openOrCreateDatabase("Lists", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS task(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, job VARCHAR,schedule VARCHAR, tme VARCHAR);");
    }

}


The 'tasks' table contains 4 columns : id,job,schedule,tme
How exactly to display the data in the listview is what I'm stuck at. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Display data from Sqlite in ListView

#2 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 279
  • Joined: 23-September 15

Re: Display data from Sqlite in ListView

Posted 15 May 2017 - 11:55 PM

Have you solved ?
Was This Post Helpful? 0
  • +
  • -

#3 Soumikbhat  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 01-September 13

Re: Display data from Sqlite in ListView

Posted 27 May 2017 - 08:55 AM

View Postwseng92, on 15 May 2017 - 11:55 PM, said:

Have you solved ?


package com.example.hp.reminder;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class viewall extends AppCompatActivity {

  
   final DatabaseHandler db = new DatabaseHandler(this);
    private Cursor c;
    private static final String SELECT_SQL = "SELECT * FROM task";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewall);
        openDatabase();
        
        showRecords();

    }
    protected void showRecords() {
        Log.d("Reading: ", "Reading all contacts..");
        List<jobs> job = db.getAllJobs();
       
        ArrayList<String> total = new ArrayList<String>();
        for (jobs j : job)
        {
            
            String k = "";
            String l =k.concat("\n\nId = "+j.getID()+"\nJob Name = "+j.getName()+"\nSchedule = "+j.getSchedule()+"\nTime = "+j.getTime()+"\n===============\n\n"); //k.concat(String.valueOf(j.getID()));
          
            total.add(l.toString());
            

        }
       
        Log.d("Size : ",String.valueOf(total.size()));

        ListView l;
        l=(ListView)findViewById(R.id.lv);
        l.setEnabled(true);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, total);

       // l.setAdapter(new ArrayAdapter<String>(viewall.this,android.R.layout.simple_list_item_1,total));
        l.setAdapter(adapter);
    }
        
    protected void openDatabase(){
       // db=openOrCreateDatabase("Lists", Context.MODE_PRIVATE, null);
        //db.execSQL("CREATE TABLE IF NOT EXISTS task(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, job VARCHAR,schedule VARCHAR, tme VARCHAR);");
    }

}


Was This Post Helpful? 0
  • +
  • -

#4 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 279
  • Joined: 23-September 15

Re: Display data from Sqlite in ListView

Posted 28 May 2017 - 08:53 AM

Great you solved
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1