find Position in Cursor - Android Database

Android examples for Database:Cursor

Description

find Position in Cursor

Demo Code


//package com.java2s;
import android.database.Cursor;

public class Main {
    public static int findPosition(Cursor c, int columnIndex, int value) {
        if (c == null || columnIndex < 0) {
            return -1;
        }//from w w  w . ja v  a  2s  . c  o  m
        while (c.moveToNext()) {
            try {
                if (c.getInt(columnIndex) == value) {
                    return c.getPosition();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    public static int findPosition(Cursor c, int columnIndex, long value) {
        if (c == null || columnIndex < 0) {
            return -1;
        }
        while (c.moveToNext()) {
            try {
                if (c.getLong(columnIndex) == value) {
                    return c.getPosition();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    public static int findPosition(Cursor c, int columnIndex, String value) {
        if (c == null || columnIndex < 0) {
            return -1;
        }
        while (c.moveToNext()) {
            try {
                if (c.getString(columnIndex).equals(value)) {
                    return c.getPosition();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return -1;

    }
}

Related Tutorials