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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:35:09+00:00 2026-05-26T18:35:09+00:00

I’ve created a custom ListView with checkboxes. I wanted to delete the selected items

  • 0

I’ve created a custom ListView with checkboxes. I wanted to delete the selected items on “Delete” menu option selection.
I am handling the onCheckedChanged event on CheckBox. Here I maintain an ArrayList to note down the selected elements position. Now when the user selects the “Delete” menu item I do remove the choosen items from ListAdapter’s ArrayList.

Below is my code,

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    names = new ArrayList<String>();
    names.add("first");
    names.add("second");
    names.add("three");
    names.add("four");
    names.add("five");
    names.add("six");
    names.add("seven");
    names.add("eight");
    names.add("nine");

    listAdapter = new SelfAdapter(this,names);

    this.setListAdapter(listAdapter);                       
}

    @Override
public boolean onOptionsItemSelected(MenuItem menuItem){
    int menuId = menuItem.getItemId();
    ArrayList<Integer> selectedItems;
    switch(menuId){
    case R.id.delitem:
        Log.d(TAG,"del item selected");
        selectedItems = listAdapter.getSelected();
           **//pick the selected position items and delete them from the
           // listadapter arraylist**
        for(Integer element: selectedItems){
          Log.d(TAG,"Selected:"+element.toString());
          names.remove(element.intValue());
          listAdapter.notifyDataSetChanged(); 
        }          
    }
    return true;
}

Here is the code for custom adapter,

class SelfAdapter extends ArrayAdapter<String>{
ViewHolder holder = new ViewHolder();
private Activity context;
private ArrayList<String> names;
private String TAG = "SelfAdapter"; 
ArrayList<Integer> checkedPos; **//contains the selected items position**
public SelfAdapter(Context context, ArrayList names) {
    super(context,R.layout.checkboxnlist, names);
    this.context = (Activity) context;
    this.names = names;
    checkedPos = new ArrayList<Integer>();
}

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

    final int listPosition = position;

    if(convertView == null){
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.checkboxnlist,parent,false);            
        holder.chkBox = (CheckBox)convertView.findViewById(R.id.chkvw);
        holder.txtView = (TextView)convertView.findViewById(R.id.txtvw);
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtView.setText(names.get(position));
    holder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d(TAG,"listPosition:"+listPosition);
          if(isChecked){
              checkedPos.add(new Integer(listPosition));
          }
          else{
              checkedPos.remove(listPosition);
          }
        }
    });
    return convertView;
}

public ArrayList<Integer> getSelected(){
    return checkedPos;
}

static class ViewHolder{
    TextView txtView;
    CheckBox chkBox;
}

}

Now the problem is that if select 1st,2nd items (for example) then I see the wrong items get deleted and the listview populate after this is completely wrong. Later if I attempt to delete the items it’s throwing “Index out of bounds exception”. I guess the code is going wrong in getView method.

Can some one help me in finding out what am doing wrong here.

Thanks

  • 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-26T18:35:09+00:00Added an answer on May 26, 2026 at 6:35 pm

    When you remove elements from names, you also need to remove elements from the checkedPos array. Otherwise, the two arrays will have different lengths. Then when you try again, checkedPos will be longer than names and you end up with the index out of bounds exception.

    Also, when you are deleting from names, you can’t use a normal iterator through . If you are supposed to delete names at indexes 1 and 2, then when you delete name 1, name 2 moves up to position 1. But your indexing isn’t updated accordingly. The easiest solution is to iterate backwards through the position array, deleting from the end of names before deleting from the beginning.

    P.S. It’s a little weird to switch from ordinal numbers (“first”, “second”) to cardinal numbers (“three”, “four”, etc.) in the middle of the list.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
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.