从android中的数据库读取到arraylist

编程入门 行业动态 更新时间:2024-10-27 10:23:19
本文介绍了从android中的数据库读取到arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我对 Android 开发非常陌生,无法弄清楚如何从我的 database/table 读取到 arrayList.现在我有:

I'm very new to android development and can't figure out how to read from my database/table into an arrayList. Right now I have:

      Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);

       int Column1 = c.getColumnIndex("Field1");
       int Column2 = c.getColumnIndex("Field2");

       // Check if our result was valid.
       c.moveToFirst();
       if (c != null) {
        // Loop through all Results
        do {
         String Name = c.getString(Column1);
         int Age = c.getInt(Column2);
         Data =Data +Name+"/"+Age+"\n";
        }while(c.moveToNext());
       }
       TextView tv = new TextView(this);
       tv.setText(Data);
       setContentView(tv);
      }
      catch(Exception e) {
       Log.e("Error", "Error", e);
      } finally {
       if (myDB != null)
        myDB.close();
      }

如何将其读入数组列表?

How do I read this into an arraylist?

推荐答案

这是一段代码,向您展示了如何从数据库查询中获取 ArrayList.

Here is a code which shows you how to get ArrayList from Database query.

public ArrayList<String> GetAllValues(String aTable,String[] aColumn)
{
    ArrayList<String> list = new ArrayList<String>();
    Cursor cursor = sqlDB.query(aTable, aColumn, null, null, null, null, null);
    if (cursor.moveToFirst())
    {
        do
        {
            list.add(cursor.getString(0));
        }
        while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) 
    {
        cursor.close();
    }

    return list;
}

如果您在aColumn"中提供空值,它将为您提供所有列.我希望它会有所帮助.

If you provide null in "aColumn", it will give you all columns. I hope it will help.

这篇关于从android中的数据库读取到arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 10:40:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/885048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   android   arraylist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!