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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:49:26+00:00 2026-06-11T06:49:26+00:00

I have searched almost every possible question related to this topic but not able

  • 0

I have searched almost every possible question related to this topic but not able to resolve this issue. Whenever I try to delete items via custom delete button embedded with ListView item, after deleting few items it gives me error of IndexOutOfBound error. I know this error is coming due to list being not able to refresh itself after item has been removed with position. Here is my code:

MainActivity:

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity 
{

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

        ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,getModel());
            //adapter.clear();
        setListAdapter(adapter);
    }

    private List<Model> getModel() {
        List<Model> list = new ArrayList<Model>();
        list.add(get("Linux"));
        list.add(get("Windows7"));
        list.add(get("Suse"));
        list.add(get("Eclipse"));
        list.add(get("Ubuntu"));
        list.add(get("Solaris"));
        list.add(get("Android"));
        list.add(get("iPhone"));
        // Initially select one of the items
        //list.get(1).setSelected(true);
        return list;
      }

    private Model get(String s)
    {
        return new Model(s);
    }

}

Code for InteractiveArrayAdapter.java:

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;
    private ListView lv;
    private MainActivity ma;


    public InteractiveArrayAdapter(Activity context, List<Model> list)
    {
        super(context, R.layout.rowbuttonlayout, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder
    {
        protected TextView text;
        protected Button btn;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;

        if (convertView == null) 
        {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);


            final ViewHolder viewHolder = new ViewHolder();

            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.btn = (Button) view.findViewById(R.id.button1);

            viewHolder.btn.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {

                        list.remove(position);
                        notifyDataSetChanged();  

                }

            });

            view.setTag(viewHolder);
            viewHolder.btn.setTag(list.get(position));
        } //when convertView = null
        else 
        {
            view = convertView;
            ((ViewHolder) view.getTag()).btn.setTag(list.get(position));
            //((ViewHolder) view.getTag()).text.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        //holder.btn.setChecked(list.get(position).isSelected());
        return view;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Model getItem(int position) {
        return (null==list)? null: list.get(position);
    }


}

Code for Model Class:

public class Model 
{
    private String name;

    public Model(String name) {
    this.name = name;

    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }
}

Any help regarding this would be great.

  • 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-11T06:49:28+00:00Added an answer on June 11, 2026 at 6:49 am

    Uncomment the following line

        //((ViewHolder) view.getTag()).text.setTag(list.get(position));
    

    Replace your remove statement with the following one.

        list.remove(viewHolder.btn.getTag());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched for a general solution to this but only find answers to
I have searched and searched for this but I think my terminology isn't correct
I have searched stackoverflow for a question like mine but most of the questions
I have searched almost a month everyday for this. In some cases they use
I searched for a similar question, but haven't seen anyone ask this before. I've
I have searched for this, and i found that for api less than 12,
I have searched apple's documentation and other posts on Stack Overflow, but I'm still
I have searched around but have been unsuccessful in determining whether the following approach
I have searched for two weeks, every night, exhaustively reading forum threads and trying
I have searched online for a while and almost all the questions regarding image

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.