Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:32:34+00:00 2026-06-12T02:32:34+00:00

I need a help with my android simple app. I tried so much to

  • 0

I need a help with my android simple app. I tried so much to solve it but its useless.
When I am using the additem() method I got an exception:

android.database.cursorindexoutofboundsexception index 0 requested with a size of 0

the database code

public class DBAdapter extends SQLiteOpenHelper 
{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Items_Manger";
private static final String Table = "Items";
private static final String creat_Table=
"CREATE TABLE if not exists Items(id  integer primary key autoincrement,name   text,price INTEGER)";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_price = "price";

public DBAdapter(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(creat_Table);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Table);


    onCreate(db);
}



void additem(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ID, contact.get_id());
    values.put(KEY_NAME, contact.get_name()); 
    values.put(KEY_price, contact.get_price());

    db.insert(Table, null, values);      
    db.close(); 
}


 Contact getitem(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(Table, new String[] { KEY_ID,
            KEY_NAME, KEY_price }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();
    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
            Integer.parseInt(cursor.getString(2)));
    // return contact
    return contact;
}


public List<Contact> getallitems() {
    List<Contact> contactList = new ArrayList<Contact>();

    String selectQuery = "SELECT  * FROM " + Table;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);


    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.set_id(Integer.parseInt(cursor.getString(0)));
            contact.set_name(cursor.getString(1));
            contact.set_price(Integer.parseInt(cursor.getString(2)));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }


    return contactList;
}

//**************************************************************
public int updateitem(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.get_name());
    values.put(KEY_price, contact.get_price());


    return db.update(Table, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.get_id()) });
}

//**************************************************************
public void deleteitem(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(Table, KEY_ID + " = ",new String[] { String.valueOf(contact.get_id()) });           
    db.close();
}

the button :

    insert.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            int idd= Integer.parseInt(eid.getText().toString());
            String namee= ename.getText().toString();
            int pricee = Integer.parseInt(eprice.getText().toString());
            Contact c = new Contact();
            c=db.getitem(idd);
            db.additem(new Contact(idd,namee,pricee));  


        }
    });

the Contact class

public class Contact {
int id;
int price;
String name;

public Contact(){}

public Contact(int _id, String _name, int _price){
this.id = _id;
this.name = _name;
this.price=_price;
}
public Contact(String _name, int _price){
this.name = _name;
this.price=_price;
}
public Contact(int _id){
this.id=_id;
}

public int get_id(){return this.id;}
public int get_price(){return this.price;}
public String get_name(){return this.name;}

public void set_id(int _id){this.id=_id;}
public void set_price(int _price){this.price=_price;}
public void set_name(String _name){this.name=_name;}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:32:36+00:00Added an answer on June 12, 2026 at 2:32 am

    That exception means that you are trying to read an empty Cursor (“index 0 with size of 0”).

    The problem is that SQliteDatabase#query() will always return a Cursor, the Cursor may be empty but it will not be null. So let’s change getItem() to return a null value when a Contact does not exist already:

    Contact getitem(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(Table, new String[] { KEY_ID,
                KEY_NAME, KEY_price }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
    
        Contact contact = null;
        if (cursor.moveToFirst()) 
            contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), Integer.parseInt(cursor.getString(2)));
    
        // return contact
        return contact;
    }
    

    Now update your Button’s OnClickListener:

    Contact c = db.getitem(idd);
    if(c == null)
        db.additem(new Contact(idd,namee,pricee));  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help... I got errors with my android xml... Here's the code: <?xml
I'm creating an Android app utilising OpenGL ES, and need some help with the
I am trying to create my first Android app and need some help. Basically
My Android app reads a huge XML file using Simple XML . Obviously it
I need help with Android. I'm trying to create a slot machine game. So,
I need help on encryption/decryption on Android application. I explain the situation. I'm actually
I am new to android and need help on this. I have two views
I need help to find the correct command, on android, to remove the blue
Hello i am new in android and i really need help with something. I
i need some help in addition to android-layouts. For eyample: Actually I use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.