本文介绍了如何将数据从SQLite数据库添加到ArrayList(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,正在制作待办事项列表应用程序.我有一个包含一列任务的数据库.如何将这些任务放入ArrayList并显示?

I'm relatively new to Android and I was making a To-Do List application. I have a database containing a column of tasks. How can I put these tasks into an ArrayList and display it ?

helper = new TaskDBHelper(MainActivity.this);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(TaskContract.TABLE, new String[]{TaskContract.Columns._ID,TaskContract.Columns.TASK}, null, null, null, null, null);

        arrayListToDo = new ArrayList<String>();

arrayAdapterToDo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListToDo);

        ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
        listViewToDo.setAdapter(arrayAdapterToDo);

推荐答案

尝试此代码

while (cursor.moveToNext()) {

arrayListToDo.add(cursor.getString(cursor.getColumnIndex("Your column name")));
}

这篇关于如何将数据从SQLite数据库添加到ArrayList(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:58