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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:51:54+00:00 2026-06-14T07:51:54+00:00

I am building an application in which I’m populating data from the database into

  • 0

I am building an application in which I’m populating data from the database into the listview of my activity. Now, on longItemClick Listener I want to delete that value from the database and update my listview in the app.

Here my code for longClickListener on the listview:

private ListView showing_history;
private ListAdapter mHistoryListAdapter;
private ArrayList<SearchHistoryDetails> searchArrayList;
private ArrayList<SearchHistoryDetails> HistoryObjArrayList;


    mHistoryListAdapter = new ArrayAdapter<String>(this,
            R.layout.mylistlayout, populateList());
    showing_history.setAdapter(mHistoryListAdapter);
    HistoryObjArrayList = new ArrayList<SearchHistoryDetails>();

/**
     * Long tap on listview to delete the selected item from the history
     * list.
     * */
    showing_history
            .setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> arg0,
                        View arg1, int arg2, long arg3) {
                    final AlertDialog.Builder b = new AlertDialog.Builder(
                            MainActivity.this);
                    b.setIcon(android.R.drawable.ic_dialog_alert);
                    b.setMessage("Delete this from history?");
                    b.setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    Toast.makeText(getApplicationContext(),
                                            "Yes", Toast.LENGTH_LONG)
                                            .show();
                                }
                            });
                    b.setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    dialog.cancel();
                                }
                            });

                    b.show();
                    return true;
                }
            });

By this code I’m inserting data into the database:

/**
 * Inserting the string when user searches for anything into the database.
 * */
public void insertHistory(SearchHistoryDetails paraHistoryDetailsPojoObj) {

    AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(
            this);

    SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj
            .getWritableDatabase();

    ContentValues contentValues = new ContentValues();

    contentValues.put(AndroidOpenDbHelper.COLUMN_NAME_HISTORY_STRING,
            paraHistoryDetailsPojoObj.getHistoryName());
    @SuppressWarnings("unused")
    long affectedColumnId = sqliteDatabase.insert(
            AndroidOpenDbHelper.TABLE_NAME_HISTORY, null, contentValues);

    sqliteDatabase.close();

}

Here is the code from which I’m retrieving data from the database and repopulating it into the list view:

public List<String> populateList() {

    List<String> HistoryNamesList = new ArrayList<String>();

    AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);

    SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

    Cursor cursor = sqliteDatabase
            .query(AndroidOpenDbHelper.TABLE_NAME_HISTORY,
                    new String[] { AndroidOpenDbHelper.COLUMN_NAME_HISTORY_STRING },
                    null, null, null, null, AndroidOpenDbHelper.ID
                            + " DESC");

    startManagingCursor(cursor);

    while (cursor.moveToNext()) {
        String ugName = cursor
                .getString(cursor
                        .getColumnIndex(AndroidOpenDbHelper.COLUMN_NAME_HISTORY_STRING));

        SearchHistoryDetails histPojoClass = new SearchHistoryDetails();
        histPojoClass.setHistoryName(ugName);

        searchArrayList.add(histPojoClass);

        HistoryNamesList.add(ugName);
    }

    sqliteDatabase.close();

    int history_db = cursor.getCount();

    if (history_db == 0) {
        history_layout.setVisibility(LinearLayout.GONE);
    } else {
        history_layout.setVisibility(LinearLayout.VISIBLE);
    }

    return HistoryNamesList;
}

Retrieving and inserting data all things are perfectly, but deleting of a particular row is not working for me. Right now I have done some action using toast and it is working, what should I do so that, the database data updates after deletion of the row from the listview. Please give me any idea to overcome this problem.

  • 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-14T07:51:56+00:00Added an answer on June 14, 2026 at 7:51 am

    Try this..,.

    b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
                DatabaseHandler db=new DatabaseHandler(getApplicationContext());
                db.delete(arg3+"");
                list.remove(position);
                YourActivity.this.adapter.notifyDataSetChanged();
      }
    });
    

    and in DatabaseHandler class

    public void delete(String id)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        db.delete(TABLE_NAME, KEY_ID + "=?", new String[]{id});
        db.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a iPhone application which depends data from an online database. To update
I am building an application which works on data from column based database. I
I am building a Monotouch application which downloads data from the server encrypted using
I'm building an application which automatically updates an online MySQL database with results from
I am building an application which would store some information into the database and
I am building an application which stores GPS locations in a SQLite database and
I'm building an MVC application which uses Windows Authentication. I want to handle a
I'm building an application which I'm also testing in Heroku. I ran into some
I am building an application which needs to connect to a central MySQL database.
In building my application which is a tab based application, from the first tab,

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.