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

  • Home
  • SEARCH
  • 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 9188161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:56:24+00:00 2026-06-17T19:56:24+00:00

I’m learning Android/Java and I have a ListView attached to a database, and contains

  • 0

I’m learning Android/Java and I have a ListView attached to a database, and contains a simple_list_item_multiple_choice check box in each row. When the user checks 1 check box and hits a delete button, that row is deleted and the ListView updates accordingly. However, if the user checks multiple rows, my app breaks.

Why does my app break when selecting multiple rows in the ListView to delete from the attached database?

public class ListViewDisplayNumbers extends ListActivity {
public void DeleteNumber() {

    NumbersDataSource datasource = new NumbersDataSource(this);
    datasource.open();

    ListView listView = (ListView) findViewById(android.R.id.numberlist);
    SparseBooleanArray checked = listView.getCheckedItemPositions();

    List<Number> values = datasource.getAllNumbers();
    ArrayAdapter<Number> adapter = new ArrayAdapter<Number>(this, android.R.layout.simple_list_item_multiple_choice, values);

    for (int i = 0; i < checked.size(); i++) {
        int position = checked.keyAt(i);
        if (checked.valueAt(i))
            datasource.deleteNumber(adapter.getItem(position));
            adapter.remove(adapter.getItem(position)); //breaks on the last one of a multiple
    }

    adapter.notifyDataSetChanged();
    setListAdapter(adapter);
    datasource.close();
}

Log cat:

01-25 12:26:06.236: E/AndroidRuntime(7140): FATAL EXCEPTION: main
01-25 12:26:06.236: E/AndroidRuntime(7140): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.util.ArrayList.get(ArrayList.java:304)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.example.hw.ListViewDisplayNumbers.deleteNumber(ListViewDisplayNumbers.java:103)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.example.hw.ListViewDisplayNumbers.onOptionsItemSelected(ListViewDisplayNumbers.java:128)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.app.Activity.onMenuItemSelected(Activity.java:2507)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:982)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:175)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AbsListView.performItemClick(AbsListView.java:1393)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3022)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AbsListView$1.run(AbsListView.java:3817)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.os.Handler.handleCallback(Handler.java:605)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.os.Looper.loop(Looper.java:137)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.app.ActivityThread.main(ActivityThread.java:4517)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.lang.reflect.Method.invoke(Method.java:511)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at dalvik.system.NativeStart.main(Native Method)
  • 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-17T19:56:25+00:00Added an answer on June 17, 2026 at 7:56 pm

    Try doing this backwards

    for (int i = checked.size()-1; i >=0;i--) {
        int position = checked.keyAt(i);
        if (checked.valueAt(i)){
            datasource.deleteNumber(adapter.getItem(position));
            adapter.remove(adapter.getItem(position)); //breaks on the last one of a multiple
         }
    }
    

    Rationale: Had a similar issue and came across this SO question which recommended to do it backwards. It would make sense though, since removing might mess up indexes if you go forwards, while if you go backwards, you shouldn’t be affecting anything as you modify the list.

    Oh, and I noticed you may have wanted to put both adapter statements in one if block.

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

Sidebar

Related Questions

I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.