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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:21:03+00:00 2026-06-01T14:21:03+00:00

I want to make a view where I can select multiple items from listview

  • 0

I want to make a view where I can select multiple items from listview and also side by side am changing the color of selected list item and saving that item into my arraylist..My list is shown as below as:

enter image description here

But when I used to scroll it.. It is showing me 1 more item selected, even I am not selecting it like:

enter image description here

But I want that only that list item color should change where I will click…

I am using the code as:

     private class ItemsAdapter extends ArrayAdapter<String> {
 List<String> items;
  Context context;
  private LayoutInflater inflater;
  public ItemsAdapter(Context context, List<String> part_array_list) {
     super( context, R.layout.part_list, R.id.label,part_array_list );

     inflater = LayoutInflater.from(context) ;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      TextView textView ; 
    String item = (String) this.getItem( position ); 

      if ( convertView == null ) {
        convertView = inflater.inflate(R.layout.part_list, null);

        // Find the child views.
        textView = (TextView) convertView.findViewById( R.id.label );


        // Optimization: Tag the row with it's child views, so we don't have to 
        // call findViewById() later when we reuse the row.
        convertView.setTag( new ListViewHolder(textView) );


      }
      // Reuse existing row view
      else {
        // Because we use a ViewHolder, we avoid having to call findViewById().
        ListViewHolder viewHolder = (ListViewHolder) convertView.getTag();

        textView = viewHolder.getTextView() ;

      }


      textView.setText( part_array_list.get(position) );      

      return convertView;


      }
      }
        /** Holds child views for one row. */
           private class ListViewHolder {

 private TextView textView ;
 public ListViewHolder() {}
 public ListViewHolder( TextView textView ) {

   this.textView = textView ;
 }

 public TextView getTextView() {
   return textView;
 }
 public void setTextView(TextView textView) {
   this.textView = textView;
 }    

}

and in OnCreate() method,

         final ArrayAdapter<String> part_list_adapter=new ItemsAdapter(AssetSearch.this, part_array_list);
        //PartNumber_List.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    PartNumber_List.setAdapter(part_list_adapter);

    PartNumber_List.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,
            long id) {
         ListViewHolder viewHolder = (ListViewHolder) v.getTag();
         viewHolder.getTextView().setBackgroundColor(R.color.result_image_border);

        String item=(String) part_list_adapter.getItem((int) id);
        });
  • 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-01T14:21:04+00:00Added an answer on June 1, 2026 at 2:21 pm

    The issue here is that you are setting the background color to the view, then when you scroll, you end up reusing that view due to using convertView. This is exactly what you should be doing, so good job there.

    But, of course, that means list items are selected when they shouldn’t be. In order to fix this, your getView() method needs to reset the background color to its default value. I don’t know what the color was originally, but I’ll assume it was transparent. So you would put:

    textView.setBackgroundColor(android.R.color.transparent);
    

    So now you’re setting the background color to its default, but if you scroll away from the selected item, then back to it, it will have a transparent background instead of the selected background. To resolve this, put an ArrayList of Integer values inside your adapter class. When an item is clicked and onItemClick() is triggered, add the item position to that ArrayList. For example, say you have:

    public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
    

    in your adapter class. Then, your onItemClick method would look like this:

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    
         ArrayList<Integer> selectedIds = ((ItemsAdapter) parent).selectedIds;
         Integer pos = new Integer(position);
         if(selectedIds.contains(pos) {
             selectedIds.remove(pos);
         }
         else {
             selectedIds.add(pos);
         }
    
         parent.notifyDataChanged();
    }
    

    So, finally, in your getView() method, add this line:

    textView.setBackground(selectedIds.contains(position) ? R.color.result_image_border : androi.R.color.transparent);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a spinner list where my users can select a
I want to make a view in a database via Java that has a
I want to make a table view in my iPhone Application.. In that table
I want to make a conversation view with ListBox, but I don't know how
I want to make my data accessible outside of my view model. So I
I have a MS SQL view that I want to make available as a
Hi I want to make my textbox's width longer. The view that is generated,
I want make a bash script which returns the position of an element from
I want make interactive application where user launches it and can do various task
I want to make an application that will grab the HTML from a website

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.