Android - 将值从 sqlite 数据库加载到数组列表

发布于 2024-10-28 03:28:28 字数 1073 浏览 1 评论 0原文

我是安卓新手。我有一个使用 SQLite DB 的应用程序。 我需要将值从数据库推送到对象类型的数组列表。 我使用的代码在这里给出。

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

这无法正常工作。没有显示任何错误,但我没有将值放入数组列表对象列表中,

请帮助我..提前致谢..

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

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

Please help me..Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪之触 2024-11-04 03:28:29

试试这个:

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

try this:

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;
}
婴鹅 2024-11-04 03:28:29

我在使用光标从数据库检索值并在屏幕上显示时遇到了类似的问题。以下解决方案对我有用。

            Cursor cur = db.getAllValues();
            int index = c.getColumnIndex("date");
    cur.moveToFirst();

    while (cur.isAfterLast() == false) {
        System.out.println(cur.getString(index)); // For debug purposes
        String date = cur.getString(index);
        try {
            ob = new objectname();
            ob.setDate(date);
            resultList.add(ob);
        } catch (Exception e) {
            Log.e(MY_DEBUG_TAG, "Error " + e.toString());
        }
        cur.moveToNext();
    }
    cur.close();

I had a similar issue with retrieving values from the db using cursors and displaying on screen.. The below solution works for me..

            Cursor cur = db.getAllValues();
            int index = c.getColumnIndex("date");
    cur.moveToFirst();

    while (cur.isAfterLast() == false) {
        System.out.println(cur.getString(index)); // For debug purposes
        String date = cur.getString(index);
        try {
            ob = new objectname();
            ob.setDate(date);
            resultList.add(ob);
        } catch (Exception e) {
            Log.e(MY_DEBUG_TAG, "Error " + e.toString());
        }
        cur.moveToNext();
    }
    cur.close();
当爱已成负担 2024-11-04 03:28:29

可能您的查询正在绘制空白,并且 moveToFirst 返回 false。

if 语句之前记录 c.getCount() 的值怎么样?

Probably your query is drawing a blank and moveToFirst is returning false.

What about logging the value of c.getCount() before your if statement?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文