get Id List From Cursor - Android Database

Android examples for Database:Cursor Get

Description

get Id List From Cursor

Demo Code


//package com.java2s;
import android.database.Cursor;
import android.provider.BaseColumns;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Long> getIdListFromCursor(Cursor cursor) {
        long id = -1;
        List<Long> idList = new ArrayList<>();

        if (cursor != null && cursor.moveToPosition(-1)) {
            while (cursor.moveToNext()) {
                id = cursor.getLong(cursor
                        .getColumnIndexOrThrow(BaseColumns._ID));
                if (id >= 0) {
                    idList.add(id);//from ww  w .ja  v  a 2s.c om
                }
            }
            return idList;
        }
        return null;
    }
}

Related Tutorials