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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:45:53+00:00 2026-05-30T14:45:53+00:00

I have an SQLite table and in the certain activity I obtain all the

  • 0

I have an SQLite table and in the certain activity I obtain all the names fom the table and populate a listview with these names.

Inside the listview listener, the user have can delete the selected item.

The problem is when I delete the item the app crashes.

Please take a look on my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylocations);

    tv_counter = (TextView) findViewById(R.id.counter);
    tv_testCounter = (TextView) findViewById(R.id.testCounter);
    lv = (ListView) findViewById(R.id.mylist);

    mpoh = new MP_DB(this);
    db = mpoh.getWritableDatabase();
    cv = new ContentValues();


       if (hasRecords()) {
           Toast.makeText(getBaseContext(), getRowsNum()+" row(s)", Toast.LENGTH_SHORT).show();
           get_MPNames();
           arrayToArrayList();
           setListView();
           lv.setOnItemClickListener(listViewListener);
       } else {
           Toast.makeText(getBaseContext(), "NO RECORDS"+","+getRowsNum()+"rows", Toast.LENGTH_SHORT).show();
       }
}

Here are the method to convert the array to arraylist, and the listview listener:

private void arrayToArrayList() {
    int s = str.length;
    al = new ArrayList<String>();
    for (int i=0; i < s; i++) {
        al.add(str[i]);
    }
}

private int getRowsNum() {
    return mpoh.getCurrentRowNumber();
}

OnItemClickListener listViewListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        pos = arg2;
        showDialoge();          
    }
};

Here how I delete element from the DB and the ListView:

private void deleteMPfromListView(int pos) {
    al.remove(pos);
    adapter.notifyDataSetChanged();
    Toast.makeText(getBaseContext(), al.size()+" rows left in list view", Toast.LENGTH_SHORT).show();
}

private void deleteMPFromDB(int pos) {
    mpoh.deleteMP(pos);
    Toast.makeText(getBaseContext(), getRowsNum()+" rows left in DB", Toast.LENGTH_SHORT).show();
}

private Boolean hasRecords() {
    if (getRowsNum() == 0) {
        return false;
    } else {
        return true;
    }
}

private void setListView() {

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
            android.R.id.text1, al);
    lv.setAdapter(adapter);       
}

private void get_MPNames() {

    str = new String[getRowsNum()];

    for (int i=0; i <= getRowsNum()-1; i++) {
        str[i] = mpoh.getMP_Name(i+1);
    } //tv_testCounter.setText(str[87]);
}
  • 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-05-30T14:45:54+00:00Added an answer on May 30, 2026 at 2:45 pm

    Removing from the database has nothing to do with removing them from the ListView. I have not code of your implementation but you may try something like this too dynamically add or remove items:

    public class LVDemo extends ListActivity {
    
    // handles the ListView data
    ArrayAdapter<String> adapter;
    
    // Items that are displayed
    ArrayList<String> listItems=new ArrayList<String>();
    
    @Override
    public void onCreate(Bundle icicle) {
    
        super.onCreate(icicle);
        setContentView(R.layout.main);
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            listItems);
        setListAdapter(adapter);
    }
    
    /**
     * Remove item.
     */
    public void removeItem(int index) {
        listItems.remove(index);
        adapter.notifyDataSetChanged();
    }
    

    }

    In General: You change the the ArrayList containing the element and then notify the adapter for the ListView

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have 3 rows in a sqlite table. While reading these 3 rows
I have certain sqlite data having the certain code. String sql = create table
I have a sqlite database with a table named Achievements, it stores whether certain
I have Price column in my SQLite table (OrderTable). I want to add all
I have an SQLite table that contains a BLOB I need to do a
I have a SQLite table called posts . An example is shown below. I
I have a sqlite (v3) table with this column definition: timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
I have a boolean (BOOL) type field in the SQLite table. In the SubSonic
I have a table in SQLite: CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)
I have a DataTable which I want to save to a SQLite Database Table.

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.