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 7725185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:52:33+00:00 2026-06-01T04:52:33+00:00

Question I am trying to use a button to delete the first entry to

  • 0

Question


I am trying to use a button to delete the first entry to my database. I thought I had the code right but it does not seem to be deleting any of the data, when the delete button is pressed android is setup so which this as it onClick method:-

public void delete(View view)
 {
     datasource.open();
     datasource.deleteTitle(0);
     datasource.close();
 }

the delete method it is calling is this method here:-

 public boolean deleteTitle(long rowId) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + 
            "=" + rowId, null) > 0;
}

is there a different way I should be doing this?

The button is set up like this :-

   <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete First" 
        android:onClick="delete"/>

I am using the built in onClick to go to the method I want. I am using this in other parts of the application and it works just fine.

Solution:


From the accepted answer, I took the code in that and created a call in the DatabaseHelper class names delete first. The code I used was :-

public void deleteFirst()
{
    Cursor cursor = db.query(DATABASE_TABLE, null, null, null, null, null, null); 

    if(cursor.moveToFirst()) {
        long rowId = cursor.getLong(cursor.getColumnIndex(KEY_ROWID)); 

        db.delete(DATABASE_TABLE, KEY_ROWID +  "=" + rowId, null);
   }
}

and I then changed the code I gave above to:

public void delete(View view)
 {
     datasource.open();
     datasource.deleteFirst();
     fillData();
     datasource.close();
 }

with the fillData() method refreshing the view.

  • 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-01T04:52:35+00:00Added an answer on June 1, 2026 at 4:52 am

    The first record in the database will not always have an ID of “0”. Each time you insert a new record the primary key will increment by 1. So what you need is to get the id of the first record, and perform the delete using that id.

    Cursor cursor = db.query(DATABASE_TABLE, null, null, null, null, null, null); 
    
        if(cursor.moveToFirst()) {
            long rowId = cursor.getLong(cursor.getColumnIndex(KEY_ROWID)); 
    
            db.delete(DATABASE_TABLE, KEY_ROWID +  "=" + rowId, null);
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[I posted a similar question on serverfault.com, but had very little feedback so trying
I'm trying to use the following code to press a button on my other
I'm trying to use the replace function in JavaScript and have a question. strNewDdlVolCannRegion
This is the reciprocal to this question . Currently, I am trying to use
Question I'm trying to create a basic wxWidgets program with a text entry box,
Not sure the title fully describes the problem/question I'm trying to ask, sorry. One
I'm trying to use jQuery to add dynamic Add/Remove row function, but I meet
I am trying to refresh a page, but WANT to use the cache and
Question. I'm trying to use SQL to update a listview in C#. Using the
I'm trying to use jQuery to load a PartialView. It does this fine at

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.