I have to understand some java code from android studio, but as I'm new to both Java and android I have a question.
Anyone know what Cursor actually is and what does it do exactly in this case?
private Movie cursorToMovie (Cursor cursor)
{
Movie movie = new Movie();
movie.setId(cursor.getInt(0));
movie.setName(cursor.getString(1));
movie.setGenre(cursor.getString(2));
movie.setYear(cursor.getInt(3));
movie.setSynopsis(cursor.getString(4));
return movie;
}
public List<Movie> getAllMovies()
{
open();
List<Movie> movieList = new ArrayList<>();
Cursor cursor = db.query(Movie.TABLE_NAME, MovieAllColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
Movie movie = cursorToMovie(cursor);
movieList.add(movie);
cursor.moveToNext();
}
cursor.close();
close();
return movieList;
}
Cursor is an interface which represents a 2 dimensional table of any database. When you try to retrieve some data using SELECT statement, then the database will first create a Cursor object and return its reference to you.
The pointer of this returned reference is pointing to the 0th location which is otherwise called as before first location of the Cursor, so when you want to retrive data from the cursor, you have to first move to the first record so we have to use moveToFirst
When you invokes moveToFirst() method on the Cursor, it takes the cursor pointer to the first location. Now you can access the data present in the first record
The above code shows that you are trying to read the data but there is some missing information.