【问题标题】:How to add SQLite rows to an ArrayList<>如何将 SQLite 行添加到 ArrayList<>
【发布时间】:2014-04-02 12:41:22
【问题描述】:

我从我的 SQLite-DB 表中检索所有行

Cursor  cursor = db.rawQuery("select * from table",null);

然后我想将每一行添加到ArrayList

请帮忙!

【问题讨论】:

    标签: android sqlite arraylist


    【解决方案1】:

    执行此操作的代码如下所示,具体取决于您的表中的内容。您必须创建一个能够在表中表示行的类。

    例如,如果您的表中有人,具有姓名和年龄,您将需要一个对象 Human(String name, int age) 放入列表中。

    SQLiteDatabase db = helper.getReadableDatabase();
    ArrayList<Human> results = new ArrayList<Human>();
    
    try {
    
        String rawQuery = "Select name, age from people";
    
        Cursor c = db.rawQuery(rawQuery, null);
    
        try {
    
            if (!c.moveToFirst())
                return null;
    
            do {
                results.add(new Human(c.getString(0),c.getInt(1)));
            } while (c.moveToNext());
        } finally {
            c.close();
        }
    } finally {
        db.close();
    }
    
    
    
    public class Human {
        private String name;
        private int age;
    
        public Human (String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
    

    【讨论】:

    • 您能否详细说明一下 Human、Name、Age 示例,并将其调整为上述解决方案。
    • @eMM 完成,更新代码并提供 Human 类以及适当的 sql 查询
    • 创意解决方案丹尼尔。 +1
    【解决方案2】:

    如果您使用的是自定义对象数组,则使用如下:

    public void fillDataToArray() {
    
                ArrayList<BeanClass> arrayList = new ArrayList<BeanClass>();
                DatabaseHandler db = new DatabaseHandler(getActivity());
                db.open();
                try
            {
                Cursor c = db.getImageNameForList();
    
                    if (c != null && c.getCount() > 0) {
                    c.moveToFirst();
                    for (int count = 0; count < c.getCount(); count++) {
                         Beanclass detail = new Beanclass ();
    
                         detail.setName(c.getString(c
                         .getColumnIndex(DatabaseHandler._ID)));
    
                         detail.setPath(c.getString(c
                         .getColumnIndexOrThrow(DatabaseHandler._PATH)));
    
                         arrayList.add(detail);
                        c.moveToNext();
    
                        }
                    }
            }
        catch(Exception e)
            {
            e.printStackTrace();
            }
                c.close();
                db.close();
    
            }
    

    如果您在活动中使用它,请将getActivity() 更改为本地上下文或您在活动中使用上下文的任何方式。

    数据库适配器类中的数据检索方法:

    > public Cursor getImageNameForList() {         
    return db.rawQuery("select " + IMAGE_PATH + " from "+ TABLE_PRODUCT_IMAGES , null);     
    }
    

    【讨论】:

    • 如果我想在 TextView 中显示 ArrayList 中第二项的“图像 ID”,我该怎么做?我可以创建我只想要JAVA部分的xml布局。
    • textView.setText(arrayList.get(1).getImageID().toString());这里“1”是arraylist第二项的位置/索引。
    【解决方案3】:

    在您的 Db Adapter 类中,添加此方法

    public ArrayList<String>getData()
    {
        ArrayList<String>names=new ArrayList<String>();
        Cursor c = getReadableDatabase().rawQuery("Select * from tableName", null);
        c.moveToFirst();
        do
        {
            String s1=c.getString(c.getColumnIndex("<Column_Name>"));
            names.add(s1);
        }while(c.moveToNext());     
        return names;
    }
    

    然后在你的活动类中添加这个

    ArrayList<String> cid;
    DB_Adapter database = new DB_Adapter(getApplicationContext(), "<your schema name>", null, 1);
    cid=database.getData();
    

    之后,将您的数组列表添加到设置为 ListView 的数组适配器中

    【讨论】:

    • 请问“”是什么?
    • 它是你的数据库的名字,你可以给任何你想要的名字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2020-11-08
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多