I want to retrieve the data from cursor when pressing the button. For the first time I want to show first row data, after pressing button I need to show second row data. How can I achieve this? I have more number of rows in my database.
While I am using following code I am getting last row data.
for(int i=0;i<cursor.getColumnCount();i++){
String path=c.getString(2);
Bitmap bitmap=BitmapFactory.decodeFile(path);
title.setText(c.getString(1));
brewerimage.setImageBitmap(bitmap);
}
For the first timebutton click,
cursor.moveToFirst();
Then keep on using,
cursor.moveToNext()
until end of the row.
To be more specific:
if (cursor.moveToFirst()) {
do {
String path=c.getString(2);
Bitmap bitmap=BitmapFactory.decodeFile(path);
title.setText(c.getString(1));
brewerimage.setImageBitmap(bitmap);
} while(cursor.moveToNext();
}
You forgot to move your cursor to first row. Please try this:
if (cursor.moveToFirst())
{
do
{
String path=cursor.getString(2);
Bitmap bitmap=BitmapFactory.decodeFile(path);
title.setText(cursor.getString(1));
brewerimage.setImageBitmap(bitmap);
} while (cursor.moveToNext());
}
You can take first row of data like this
Replace 0 in
cursor.getString(0);
with your value;
if (cursor.moveToFirst())
{
do
{
String path=cursor.getString(0);
title.setText(path);
} while (cursor.moveToNext());
}
Try this hope it will help you:
try {
if (cursorOne.moveToFirst()) {
String path=c.getString(2);
Bitmap bitmap=BitmapFactory.decodeFile(path);
title.setText(c.getString(1));
brewerimage.setImageBitmap(bitmap);
//no need to look up column index since you already know it due to the projection used
}
} finally {
cursorOne.close();
}
Try this..
if (cursor.getCount() > 0) {
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String path=c.getString(2);
Bitmap bitmap=BitmapFactory.decodeFile(path);
title.setText(c.getString(1));
brewerimage.setImageBitmap(bitmap);
}
}
After read your comments i have one idea. That is leave the query as like now.
Step 1
Save your data in static ArrayList or POJO class
Step 2:
Whenever you want to click button just increase the cont +1 and save it. Use this count as position and easily get values.
You can get particular at the given position as like.
public void getRowAt(int posAt){
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+tableName+" LIMIT 1 OFFSET "+posAt, null);
int count = cursor.getCount();
while (cursor.moveToNext()){
// here your can get your row data.
}
cursor.close();
db.close();
}
Are you using database so you getting record from database am i right ?
so you getting record in cursor so from which column you getting data you have to pass that column name in your cursor so you are getting result .
example :
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
myDatarecordModel = new DataRecordModel();
myDatarecordModel.setStrDataRecordingID(cursor.getString(cursor.getColumnIndex(AppStackFields.DATARECORDING_ID)));
} while (cursor.moveToNext());
}
cursor.close();
Related
My query return 198 registers but cursor only read one register.
Why?
The mCount property of Cursor show 198.
This code:
public ArrayList<EnderecoOficina> retornarListaEnderecoOficina(String sCampo,
String sWhere) {
ArrayList<EnderecoOficina> lista = new ArrayList<EnderecoOficina>();
String query = String.format("SELECT %s FROM %s WHERE %s",
sCampo,
MyDBConstants.TABLE_OFICINAS,
sWhere);
SQLiteDatabase db = dbHandler.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
int i = 0;
if (cursor != null && cursor.moveToFirst()){
EnderecoOficina item = new EnderecoOficina(i++,
cursor.getString(0),
cursor.getString(1),
cursor.getString(2));
lista.add(item);
} while (cursor.moveToNext());
cursor.close();
db.close();
return lista;
}
image link (my points not allow attach image here).
I think you're confusing while syntax.
while (cursor.moveToNext());
Will loop without doing anything until the Cursor is empty. I think you wanted a do/while as explained by CommonsWare answer.
In my opinion, this is an unnecessary complicated way to do it. A lot of people don't know how to use Android Cursor. I've seen all kinds of complicated ways to do it (checking for null, moving to first, moving to index...), but this is the simplest way:
try {
// Loop while there are records on the Cursor
while (cursor.moveToNext()) {
EnderecoOficina item = new EnderecoOficina(i++,
cursor.getString(0),
cursor.getString(1),
cursor.getString(2));
lista.add(item);
}
} finally {
// Make sure the cursor is closed no matter what
cursor.close();
}
There's no need to check for null, Android Cursor API never returns a null cursor. You also need to close the Cursor once you've finished with it.
Now I want to access an entire column from database and then compare it with some text that I have stored...but I am getting no idea on how to do that..So can someone please help me with this...
Entire column? You mean all the values for a given column accross all records?
You should iterate the ResultSet obtained and start comparing the values (for example - if you iterate using "rs" object of ResultSet, you should compare:
String valueFromDB = rs.getString("myColumn");
String someTextStored = .... //the text being stored
if (valueFromDB != null) {
if (someTextStored.equals(valueFromDB) {
//Comparison succeeded - implement some logic here for handling success
}
}
And the above code should be inside a loop code that iterates over the ResultSet and
uses the "next" method to obtain the next record.
Hope this helps you
public ArrayList<String> getValues(){
Cursor cursor = null;
SQLiteDatabase db = getReadableDatabase();
Log.v("done","getting rows ");
cursor = db.rawQuery("SELECT YourColumnName FROM "+TABLE_NAME, null);
if(!cursor.moveToFirst()){
}
else{
do {
list_values.add(cursor.getString(0));
} while (cursor.moveToNext());
}
db.close();
cursor.close();
return list_values;
}
Now you are having all the values of a particular column you can compare it from this array list.
In My Application, I am going to store name of all the available tables from my database with this code:
public ArrayList<Object> showAllTable()
{
ArrayList<Object> tableList = new ArrayList<Object>();
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
if(cursor.getString(0).equals("android_metadata"))
{
//System.out.println("Get Metadata");
continue;
}
else
{
tableList.add(cursor.getString(0));
}
}
while (cursor.moveToNext());
}
cursor.close();
return tableList;
}
Yes i am able to store it. But while i am going to display it in to ListView, it will display in such way that last created shown at last. I want to display the list of the table as Last created, display first in to ListView.
Please help me, What should I have to do in my code to set such way of displaying tables?
There are several ways to do it. The easiest is probably to change your SQL call by adding ORDER BY. Here's something you can try out:
To order the items ascending:
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name ASC";
Or descending:
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name DESC";
Alternative solution
Another option would be to do it like you're doing it right now, and instead go through the Cursor from the last to the first item. Here's some pseudo code:
if (cursor.moveToLast()) {
while (cursor.moveToPrevious()) {
// Add the Cursor data to your ArrayList
}
}
before return the values just call the method
Collections.reverse(tableList);
see the full code
public ArrayList<Object> showAllTable()
{
ArrayList tableList = new ArrayList();
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
if(cursor.getString(0).equals("android_metadata"))
{
//System.out.println("Get Metadata");
continue;
}
else
{
tableList.add(cursor.getString(0));
}
}
while (cursor.moveToNext());
}
cursor.close();
Collections.reverse(tableList);
return tableList;
}
Following s my Code to display data to listview from database. But it will not work and crash my application.please Give perfect solution for my this problem.
My Code......
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.deletestudent);
// DisplayStudent();
ArrayList<String > arrlst=new ArrayList<String>();
ListView lst_stu=(ListView) findViewById(R.id.ListView01);
db.open();
Cursor cur=db.GetStudent();
for (int i=0;i<cur.getCount();i++)
{
cur.moveToFirst();
String stunm=cur.getString(1);
arrlst.add(1, stunm);
lst_stu.setAdapter((ListAdapter) arrlst);
db.close();
}
}
for (int i=0;i<cur.getCount();i++)
{
cur.moveToFirst();
String stunm=cur.getString(1);
arrlst.add(1, stunm);
lst_stu.setAdapter((ListAdapter) arrlst);
db.close();
}
The problem is that you are closing the cursor in the first iteration.
Give perfect solution for my this problem.
You look like my boss, excepts that he pays 20 dollars per hour....
Whatever, there are a lot of things which are bad in your code, mostly because you don't understand how a cursor works:
Why do you call moveToFirst on each iteration? Does it make sens for you?
Why do you close the cursor inside the for?
Why do you set the adapter on each iteration?
Why do you use an array adapter instead of CursorAdapter?
Please Try Below Code
try
{
Cursor cursor = null;
db.OpenDatabase();
cursor = db.GetStudent();
// Here if condition check wheather the cursor returns record or not.
// If cursor has some records then it will return non zero number .
if (cursor.getCount() != 0)
{
// Here if condition check wheather the cursor move to first record or not
// If it moves to first record then it will return true.
if (cursor.moveToFirst())
{
do
{
arrlst.add(cursor.getInt(cursor.getColumnIndex("your_field_in_database")).trim());
}while (cursor.moveToNext());
}
}
cursor.close();
db.closeDatabase();
lst_stu.setAdapter((ListAdapter) arrlst);
}
catch(Exception e)
{
e.printStackTrace();
}
I need to know how to retrieve data from cursor. I need this because the ringtonemanager returns all the audio files in form of cursor object, I need to know how to retrieve the values.
Anbudan.
Once you have the Cursor object, you can do something like this:
if (cursor.moveToFirst()){
do{
String data = cursor.getString(cursor.getColumnIndex("data"));
// do what ever you want here
}while(cursor.moveToNext());
}
cursor.close();
This looks a bit better:
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
...
}
Salvador's answer will continue to fetch data from the row after the last row because moveToNext() will only return false when the cursor is pointing at the row after the last row. It will continue to iterate even if the cursor is pointing at the last row.
The correct template should be:
if (cursor.moveToFirst()){
while(!cursor.isAfterLast()){
String data = cursor.getString(cursor.getColumnIndex("data"));
// do what ever you want here
cursor.moveToNext();
}
}
cursor.close();