Android - load values from sqlite database to an arraylist

后端 未结 3 1160
盖世英雄少女心
盖世英雄少女心 2021-02-04 17:34

I am new to android . I have an application working with SQLite DB. I need to push values from database to an arraylist of type object. The code i used is given here.



        
3条回答
  •  粉色の甜心
    2021-02-04 17:56

    try this:

    private ArrayList objectList  = getResults();
    
    private ArrayList getResults() {
    
        MyDb db = new MyDb(this); //my database helper file
        db.open(); 
    
        ArrayList resultList = new ArrayList();
    
    
        Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
        while (c.moveToNext())
        {
            String date = c.getString(c.getColumnIndex("date"));
    
            try
            {
                ob = new objectname();
                ob.setDate(date);// setDate function is written in Class file
                resultList.add(ob);
            }
            catch (Exception e) {
                Log.e(MY_DEBUG_TAG, "Error " + e.toString());
            }
    
        }
    
        c.close();
    
        db.close();
        return resultList;
    }
    

提交回复
热议问题