Android的 - 从SQLite数据库负荷值到ArrayList [英] Android - load values from sqlite database to an arraylist

查看:381
本文介绍了Android的 - 从SQLite数据库负荷值到ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我有一个应用程序使用SQLite数据库的工作。 我需要从数据库发送值类型对象的ArrayList。 在code,我用在这里给出。

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.

 private ArrayList<objectname> objectList  = new ArrayList<objectname>();
 //function used to get values from database
   public ArrayList<objectname> getResults() {

    MyDb db = new MyDb(this); //my database helper file
    db.open();
    Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
    if (c.moveToFirst())
    {
        do {         
            String date = c.getString(c.getColumnIndex("date"));

            try
            {
                ob = new objectname();
                ob.setDate(c.getString(c.getColumnIndex("date")));// setDate function is written in Class file
                objectList.add(ob);
            }
            catch (Exception e) {
             Log.e(MY_DEBUG_TAG, "Error " + e.toString());
            }

         } while (c.moveToNext());
    }
       db.close();
       return objectList;
}

这是无法正常工作。没有表现出任何错误,但我没有得到到ArrayList值链表

This is not working correctly. Not shown any errors but i didn't get values in to the arraylist objectList

请帮忙提前me..Thanks ..

Please help me..Thanks in advance..

推荐答案

试试这个:

private ArrayList<objectname> objectList  = getResults();

private ArrayList<objectname> getResults() {

    MyDb db = new MyDb(this); //my database helper file
    db.open(); 

    ArrayList<objectname> resultList = new ArrayList<objectname>();


    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;
}

这篇关于Android的 - 从SQLite数据库负荷值到ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆