I have a problem with getting Shop from database:
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
getShop function in ShopHandler.java
Shop getShop(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SHOPS, new String[] { KEY_ID,
KEY_SHOP }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Shop shop = new Shop(Integer.parseInt(cursor.getString(0)), cursor.getString(1)); //first problem place
return shop;
}
and part of procedure in MainClass:
public void AddToList(View view)
{
EditText search = (EditText)findViewById(R.id.editTextsearch);
String SearchProduct = search.getText().toString();
int l = 0;
int p = 0;
ShopHandler sh = new ShopHandler(this);
String[] Shops = new String[sh.getShopCount()];
float[] prices = new float[sh.getShopCount()];
for(int i=0;i<=sh.getShopCount();i++)
{
Shop shop = sh.getShop(i); //hear is second problemplace
ShopName = shop.getShop();
ProductHandler ph = new ProductHandler(this);
for(int j=0;j<ph.getProductsCount();j++)
{
if(ph.getProduct(j).getName()==SearchProduct)
{
Shops[++l]=ShopName;
prices[++p]=ph.getProduct(j).price;
}
}
}
The cursor is empty. Check moveTo...() return value before accessing row data:
Shop shop = null;
if (cursor.moveToFirst()) {
shop = new Shop(Integer.parseInt(cursor.getString(0)), cursor.getString(1)); //first problem place
}
cursor.close();
return shop;
This causes the exception. Why the cursor is empty is likely because of the off-by-one here:
for(int i=0;i<=sh.getShopCount();i++)
Either start counting from 1 or change <= to <, depending on your data.
This is most likely the issue:
for(int i=0;i<=sh.getShopCount();i++)
should be for(int i=0;i < sh.getShopCount();i++)
You will be off-by-one if you keep your first loop to stop at sh.getShopCount()
Related
this is insert code
for(int i = 0 ;pseudo.size() <= i; i++){
ContentValues values = new ContentValues();
String contact = (String) pseudo.get(i);
values.put("pseudo", contact);
values.put("adresse", "test");
values.put("image", "test");
values.put("ID", "5");
dbwrite.insert("Contact", null, values);
}
And this is read code
String[] retour = {
"pseudo",
"xmppadresse",
"image", "ID"
};
Cursor cursor = dbread.query("Contact", retour, null, null, null, null, "pseudo");
Log.d("debug", "populateWithInitialContacts: "+cursor.getCount());
while(cursor.moveToNext()) {
String pseudo = cursor.getString(cursor.getColumnIndexOrThrow("pseudo"));
Log.d("debug", "getcontact: "+pseudo);
listcontact.add(new Contact(pseudo));
}
cursor.close();
My request return nothing(It's my first time using database SQLite in Android)
for(int i = 0 ;pseudo.size() <= i; i++)
This is not how you iterate over lists. You probably want something like
for(int i = 0 ;i < pseudo.size(); i++)
I want to return all values of dbManagement.ORIGINATING_ADDRESS from this function but it just gives me last one value to blockedNumber. I know it'll give last one value but how can i get all value. kindly help
public String selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
String blockedNumber = null;
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumber;
}
In
Cursor cursor = dbManagement.selectBlockedNumbers();
the function selectBlockedNumbers() consist of following query:
cursor = db.rawQuery("SELECT " + ORIGINATING_ADDRESS + " FROM " + TABLE_BLOCK_LIST, null);
When the for-loop runs for the last time, blockedNumbers will contain the last number retrieved. So, instead of returning a String, rather returns a List<String>. In that case your code should change into
public List<String> selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
List<String> blockedNumbers = new ArrayList<String>();
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
String blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
blockedNumbers.add(blockedNumber);
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumbers;
}
public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("notification_description"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}
this is my code currently i am getting from 0 to n data in staring array i want to get in reverse array data from n to 0 so that i can Print please suggest me how i will get data
Try this method :
public ArrayList<String> getAllDescription()
{
ArrayList<String> array_list = new ArrayListString>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * " + " FROM " + NOTIFICATIONTABLE+ " DESC;", null);
res.moveToFirst();
while (res.isAfterLast() == false)
{
array_list.add(hashmap);
res.moveToNext();
}
return array_list;
}
Or If you want to reverse arrayList then you can also do it By just a single line of code .
Collections.reverse("yourArrayList");
Hope it will help you.
i am getting from 0 to n data in staring array i want to get in
reverse array data from n to 0
Why not using ASC or DESC in db query to get result in sequence as want according to notification_description column.
do it as:
cursor = db.query(NOTIFICATIONTABLE,
new String[]{"notification_id",
"notification_title",
"notification_description",
"notification_isread",
"date"}, null, null, null,
null, "notification_description DESC");
You can insert the data from end of theString[]...
public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
///////////////////////
result[(cursor.getCount()-1)-i] = cursor.getString(cursor.getColumnIndex("notification_description"));
///////////////////////
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}
I want to get data in the first column of all row from my database and convert to String[]
...
List<String> item1 = new ArrayList<String>();
// c is a cursor which pointed from a database
for(int i=0;i<=nombre_row;i++)
{
c.moveToPosition(i);
item1.add(c.getString(0));
}
String[] strarray = new String[item1.size()];
item1.toArray(strarray );
I've tried to comment step by step, and found that the problem is in the Loop for....
Please help... thanks in advance for all answer.
public String[] getData()
{
Cursor cursor = this.db.query(/your query/);
if(cursor.getCount() >0)
{
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext())
{
str[i] = cursor.getString(cursor.getColumnIndex(Columname));
i++;
}
return str;
}
else
{
return new String[] {};
}
}
I got it work :
c.moveToFirst();
do
{
item1.add(c.getString(0).toString());
//i++;
}while(c.moveToNext());
How to return a list in a column e.g like all emails in column...here my code
public String reTurn() throws SQLException
{
String emails=null;
Cursor mCursor = db.rawQuery("SELECT EmailNO FROM Details_Customer " ,null);
mCursor.moveToFirst();
if(mCursor.getCount() > 0){
emails= mCursor.getString(mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL));
//password = mCursor.getString(11);
}
return emails;
}
But it only returning one email, I want it to return all the emails in a database
Try this:
public List<String> reTurn() throws SQLException
{
List<String> emails = new ArrayList<String>();
Cursor mCursor = db.rawQuery("SELECT EmailNO FROM Details_Customer ", null);
int index = mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL);
while(mCursor.moveToNext()) {
emails.add(mCursor.getString(index));
//password = mCursor.getString(11);
}
return emails;
}
Added from comment
From this:
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ dbUser.reTurned});
I guess that your are trying to put the array of email addresses into an Intent called email. Here is a better approach:
public String[] reTurn() throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT EmailNO FROM Details_Customer ", null);
String[] emails = new String[mCursor.getCount()];
int i = 0;
int index = mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL);
while(mCursor.moveToNext()) {
emails[i++] = mCursor.getString(index);
//password = mCursor.getString(11);
}
return emails;
}
And to put this in an Intent:
email.putExtra(Intent.EXTRA_EMAIL, dbUser.reTurned());
Lastly, in your new Activity read the email array with:
String[] emails = getIntent().getStringArrayExtra(Intent.EXTRA_EMAIL);
Hope that helps.
The problem with your code is inside the if statement.
emails = mCursor.getString(...);
That statement is only being executed once, so you only get one email.
Instead try something like:
String[] emails = new String[mCursor.getCount()];
for(int i=0; i<emails.length; i++){
emails[i] = mCursor.getString(...);
mCursor.moveToNext();
}
You can use the code as follow to get all mail address :
mCursor.moveToFirst();
ArrayList<String> mails = new ArrayList<String>();
if(mCursor.getCount() > 0){
while(!mCursor.isAfterLast()){
mails.add(mCursor.getString(mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL)));
mCursor.moveToNext();
}
}
They're all available through the Cursor because it's essentially an iterator. See the Cursor API. So you can pass the cursor as you would an array and iterate over it as needed.
If truly necessary you can copy the results into an array:
int n = mCursor.getCount();
String [] emails = new String[n];
int emailColIndex = mCursor.getColumnIndex(DBAdapter.COLUMN_EMAIL);
mCursor.moveToFirst();
for (int i = 0; i < n; i++) {
emails[i] = mCursor.getString(emailColIndex);
mCursor.moveToNext();
}
// A good place to close the cursor.