1

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);
1
  • You could iterate over your Cursor and add the tasks to your ArrayList but I'd suggest you to look into CursorAdapters. Commented Dec 17, 2014 at 10:52

3 Answers 3

2

Try this code

while (cursor.moveToNext()) {

arrayListToDo.add(cursor.getString(cursor.getColumnIndex("Your column name")));
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can get the data in Array list :-

ArrayList<String> arrayListToDo= new ArrayList<String>();  
//your query will returns cursor
if (cursor.moveToFirst()) {
  do {
    arrayListToDo.add(get you string from cursor);
  } while (cursor.moveToNext());
}

now add this aray list to your Adapter.

Comments

0

get list data from database this is only overview

     ArrayList<String> data = databaseObject.GetList();
ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
arrayAdapterToDo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listViewToDo.setAdapter(arrayAdapterToDo);


      ArrayList<String> GetList()
    {
        ArrayList<String> arrayListtemp= new ArrayList<String>();
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(TaskContract.TABLE, new String[]{TaskContract.Columns._ID,TaskContract.Columns.TASK}, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                arrayListtemp.add("");// added your table value from database
            } while (cursor.moveToNext());
        }
        return arrayListtemp;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.