Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Cursor? how to read object data?

i know a Cursor has method for get String, Int, etc but isn't there somethings as

mycursor.GetObject(index)

I want to create a dynamic method which returns an object and I only cast it.

or is it possible use mycursor.GetString(index) for any type? String,Float,Double,Long,Short, Blob,Int,etc

and I can use for example for a Float or a Int or any type and cast it?

for example

(Blob) newblob=mycursor.GetString(i_return_BloB);
(Int) newint=mycursor.GetString(i_return_Int);
(Float) newfloat=mycursor.GetString(i_return_Float);
(Double) newdouble=mycursor.GetString(i_return_Double);
(Long) newlong=mycursor.GetString(i_return_long);
(Short) newshort=mycursor.GetString(i_return_short);

would it work? could i use mycursor.GetString() for any type?

like image 835
angel Avatar asked Aug 08 '13 23:08

angel


People also ask

Which method is used to get data from cursor?

Once the cursor's position is pointing to a valid row, the columns of the row can be read from the cursor. To read the data, the code in Listing 5.10 uses two methods from the cursor class: Cursor. getColumnIndexOrThrow() and one of the type get() methods from the Cursor class.

What is the use of cursor in Android?

Introduction to Cursor in Android The basic purpose of a cursor is to point to a single row of the result fetched by the query. We load the row pointed by the cursor object. By using cursor we can save lot of ram and memory.

Which function do you call from a cursor object to get the number of rows in the cursor object?

A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory. To get the number of elements of the resulting query use the getCount() method.

What is SQLiteOpenHelper?

SQLiteOpenHelper class is used for database creation and version management. For performing any database operation, you have to provide the implementation of onCreate() and onUpgrade() methods of SQLiteOpenHelper class.


2 Answers

You can get the type of a column, and then call the appropiate method from a switch statement (and build the method you're asking for by yourself).

Using getString for different types is not safe, as the docs specify:

The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.

like image 106
dst Avatar answered Sep 27 '22 17:09

dst


You want to try something like:

DataBaseHelper myDbHelper = new DataBaseHelper(this);

ArrayList<String> mArrayList = new ArrayList<String>();

String[] array;

Cursor c = myDbHelper.query(query);

int id = c.getColumnIndex("COLUMN_NAME");

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
    mArrayList.add(c.getString(id));
}

array = mArrayList.toArray(new String[0]);


System.out.println(array[0]);

c.close();

myDbHelper.close();

And your query() should look something like:

public Cursor query(String query){

    String selectAll = null;

    if(query.length() > 1){

    selectAll = "SELECT TABLENAME.COLUMNAME FROM TABLENAME WHERE TABLENAME.COLUMNNAME LIKE" + query + ";";

    return myDataBase.rawQuery(selectAll, null);
}

You need to change the SQL code to suit your needs. In this case I'm retrieving every value of a column that match the query, but you could modify the SELECT statement. Hope it helps.

like image 20
Luis Lavieri Avatar answered Sep 27 '22 16:09

Luis Lavieri