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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:33:04+00:00 2026-05-23T08:33:04+00:00

I am trying to delete the selected item from a ListView and the Problem

  • 0

I am trying to delete the selected item from a ListView and the Problem is that when I delete the selected content and refresh the Adapter the position of the remaining content changes, so because of that at some point of deleting the content again it gives IndexOutOfBoundsException. Can anyone tell me how to solve this problem?

Here is my code for deleting the selected item.

    public View getView(final int position, View convertView, ViewGroup parent) {

    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = mActivity.getLayoutInflater();
        view = inflator.inflate(R.layout.main, null);

        final ViewHolder viewholder = new ViewHolder();
        viewholder.tvTitle = (TextView) view.findViewById(R.id.txttitle);
        viewholder.imgdelete = (ImageButton) view.findViewById(R.id.imgdelete);


        viewholder.imgdelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                    list.remove(position);
                    notifyDataSetChanged();
            }
        });
        view.setTag(viewholder);
    }
    else
    {
        view  = convertView;
    }

    ViewHolder viewholder = (ViewHolder) view.getTag();
    viewholder.tvTitle.setText(list.get(position).getmTitle());

            return view;
}

Here is my Logcat error

     06-17 13:01:41.715: ERROR/AndroidRuntime(4284): FATAL EXCEPTION: main
06-17 13:01:41.715: ERROR/AndroidRuntime(4284): java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at java.util.ArrayList.remove(ArrayList.java:406)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at com.list.AdapterClass$1.onClick(AdapterClass.java:66)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at android.view.View.performClick(View.java:2408)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at android.view.View$PerformClick.run(View.java:8816)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at android.os.Handler.handleCallback(Handler.java:587)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at android.os.Looper.loop(Looper.java:123)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at java.lang.reflect.Method.invoke(Method.java:521)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-17 13:01:41.715: ERROR/AndroidRuntime(4284):     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-05-23T08:33:04+00:00Added an answer on May 23, 2026 at 8:33 am

    Try BaseAdapter instead of ArrayAdapter. Because BaseAdapter is best when you are working with custom views.

    Here is sample code..

    public class AdapterClass extends BaseAdapter{
    
            List<MyModelClass> list;
            Activity mActivity;
    
            public AdapterClass(Activity context,List<MyModelClass> objects)
            {
                    this.list = objects;
                    this.mActivity = context;
            }
    
            static class ViewHolder{
                    protected TextView tvTitle;
                    protected ImageButton imgdelete;
            }
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
    
    
                    final ViewHolder viewholder;
                    if (convertView == null) 
                    {
                            viewholder = new ViewHolder();
                            LayoutInflater inflator = mActivity.getLayoutInflater();
                            convertView = inflator.inflate(R.layout.mainn, null);
                            viewholder.tvTitle = (TextView) convertView.findViewById(R.id.txttitle);
                            viewholder.imgdelete = (ImageButton) convertView.findViewById(R.id.imgdelete);
                            convertView.setTag(viewholder);
                    }
                    else
                    {
                        viewholder = (ViewHolder)convertView.getTag();
                    }
                        viewholder.imgdelete.setOnClickListener(new OnClickListener() {
    
                                @Override
                                public void onClick(View v) {
    
                                        System.out.println("position - " + position + " size - "+ list.size());
                                        list.remove(position);
                                        notifyDataSetChanged();
                                        Toast.makeText(mActivity, "deleted", Toast.LENGTH_LONG).show();
                                }
                        });
    
                    viewholder.tvTitle.setText(list.get(position).getmTitle());
                    return convertView;
            }
            @Override
            public int getCount() {
                return list.size();
            }
            @Override
            public Object getItem(int position) {
                return position;
            }
            @Override
            public long getItemId(int position) {
                return position;
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to delete a directory that contains XML files from a remote computer.
Problem 1 I'm trying to use an Onclick event from a search button, that
Hey im trying to store a string from a selected item in a datagrid
I am trying to delete multiple selected rows from a DataGridView. When I try
I've a wpf specific problem. I'm trying to delete a Row from a Datagrid,
I'm trying to delete the messages which is selected by user by clicking the
I am trying to delete several rows from a MySQL 5.0.45 database: delete from
I'm trying to delete all digits from a string. However the next code deletes
I am trying to delete an old user from our perforce installation. A previous
i am trying to delete some files using a batch file.. (winxp) my problem

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.