Home > DeveloperSection > Forums > How to Print SQLite entries into a ListView?
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    November-04-2014 11:58 PM

 Java Android  Sqlite  ListView 
Ratings:


 1 Reply(s)
 708  View(s)
Rate this:

I am having some trouble learning the ins and outs of the SQLite world. I have some code that is allowing me to enter data into a DB. But what i want to do is return this data into a listview. At the moment all I could figure out to do was to have each row printed in a toast after a new entry is added. Can someone please show me how to alter my code to print it in a listview? Or to even look at my code and see that i am going about it in the right way. Thanks

This is the code i am using which calls a display record function

com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);

db.open();

Cursor c = db.getAllRecords();

if (c.moveToFirst())

{

      do

      {

         DisplayRecord(c);

      } while (c.moveToNext());

}

db.close();

This is the display record function

public void DisplayRecord(Cursor c)

        {

            Toast.makeText(this,"id: " + c.getString(0) + "\n" +"Item: " + c.getString(1) + "\n" +"Litres:  " + c.getString(2),Toast.LENGTH_SHORT).show();

        }

I know i need to change the second function but i dont know how to do that to make it print into a listview



Mark Devid
Mark Devid

Total Post:102

Points:714
Posted on    November-05-2014 3:10 AM

this is the code of getting data from database and insert into Arraylist and insert into arrayAdapter and than display it in listview .

i just done some editing in your existing code.


com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);

            db.open();

 

            ArrayList<String> data_list = new ArrayList<String>();

            ListView lv = (ListView)findViewById(R.id.listView1);

            Cursor c = db.getAllRecords();

            if (c.moveToFirst())

            {

                do

                {

                    data_list.add(c.getString(0));

                    DisplayRecord(c);

                } while (c.moveToNext());

            }

            ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data_list);

            lv.setAdapter(aa);

lv - is the object of ListView.


Don't want to miss updates? Please click the below button!

Follow MindStick