如何在Arraylist中检索Android Sqlite数据库

时间:2015-10-28 10:52:35

标签: android sqlite arraylist android-recyclerview

我有以下代码&希望arraylist中的所有数据都来自sqlite数据库

import android.app.Activity;

public DataSingleton()  {
    visibleSections = new ArrayList<>();
    visibleSections.add("Who We Are");
    visibleSections.add("What We Do");
    visibleSections.add("How Can You Help");
   visibleSections.add("Take a Look");
   visibleSections.add("In The News");
   visibleSections.add("Get In Touch");
  }
}

1 个答案:

答案 0 :(得分:2)

  1. 在该模型中创建一个模型,生成所需变量的getter和setter方法。
  2. public static String query_select_all = "SELECT * FROM " + TABLENAME;

    public ArrayList<model> DataSingleton()
     {
    ArrayList<visible_Model> visibleSections  = new  ArrayList<visible_Model>();
    
    
        Cursor cursor = database_helper.rawQuery(query_select_all, null);
    
        if (cursor.moveToFirst()) {
            do {
                visible_Model model = new visible_Model();
    
                model.setMessage(cursor.getString(cursor
                        .getColumnIndex(COLUMN_FROM_MSG)));
    
                visibleSections.add(model);
    
            } while (cursor.moveToNext());
    
        }
        return visibleSections;
    }
    

    在这里,我在上面创建模型名称作为visile_model并使用setMessage方法。最后我将该模型添加到arraylist中     visibleSections