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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:47:38+00:00 2026-05-28T01:47:38+00:00

How to change the background image for list items, i am able to change

  • 0

How to change the background image for list items, i am able to change only 1 item background at a time.
If there are 6 items on the list and if click on 3 items those 3 items background images should be changed, how it is possible
Below is my code

public class Places extends Activity {

    private ListView listView;


    private int selectedListItem = -1;
    private Handler mHandler = new Handler();
    private Vector<String> data; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places_custom_list_view);
        data = new Vector<String>();

        // Add data as per your requirement   
        data.add("one");
        data.add("two");
        data.add("three");
        data.add("four");
        data.add("five");
        listView = (ListView)findViewById(R.id.ListView01);
        listView.setDivider(null);

        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                selectedListItem = position;
                ((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();

                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // call any new activity here or do any thing you want here         

                    }
               }, 200L);
            }
        });

        listView.setAdapter(new EfficientAdapter(getApplicationContext()));   
    }

    private class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return data.size();
        }

        public Object getItem(int position) {
            return position;
        }

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

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

            ViewHolder holder;

            if (convertView == null || convertView.getTag() == null) {
                convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
                holder = new ViewHolder();

                holder.txtName = (TextView) convertView
                    .findViewById(R.id.name);

                convertView.setTag(holder);
            } else {
                 holder = (ViewHolder) convertView.getTag();
            }

            if(position == selectedListItem) {  
                 holder.txtName.setBackgroundResource(R.drawable.cellbghover);
            } else {
                 holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
            }

            holder.txtName.setText(data.get(position)); 

            return convertView;
        }
    }

    static class ViewHolder {
        TextView txtName;
    }
  • 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-28T01:47:38+00:00Added an answer on May 28, 2026 at 1:47 am

    Try this,it should work logically.(I didn’t try it,btw! :P)

    ...
     listView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    
    
                    EfficientAdapter.saveState.put(position,"selected");                
    
    
                    mHandler.postDelayed(new Runnable() {
    
                        @Override
                        public void run() {
                            // call any new activity here or do any thing you want here                                 
                        }
                   }, 200L);
                }
            });
    
        ...
        private class EfficientAdapter extends BaseAdapter {
    
            public static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
            private LayoutInflater mInflater;
    
            public EfficientAdapter(Context context)
            {
                mInflater = LayoutInflater.from(context);
                for(int i=0;i<5;i++)
                {
                   saveState.put(i,"unselected");
                }
            }
            public int getCount() {
                return data.size();
            }
            public Object getItem(int position) {
                return position;
            }
            public long getItemId(int position) {
                return position;
            }
            public View getView(int position, View convertView, ViewGroup parent) {
    
                ViewHolder holder;
    
                if (convertView == null || convertView.getTag() == null) {
                    convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
                    holder = new ViewHolder();
    
                    holder.txtName = (TextView) convertView
                        .findViewById(R.id.name);
    
                    convertView.setTag(holder);
                } else {
                     holder = (ViewHolder) convertView.getTag();
                }            
    
                if(saveState.get(position).equals("selected"))
                   holder.txtName.setBackgroundResource(R.drawable.cellbghover);  
                else
                   holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
    
                holder.txtName.setText(data.get(position)); 
    
                return convertView;
            }
        }
    
        static class ViewHolder {
    
            TextView txtName;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to select each list item and change the background image of the
How to change cell background image on click on cell in iphone????I have tried
I'm developing a menu. This menu, will change the background image when you click
I can't seem to figure out how to change the height of list items
I am having issues of having an image change dynamically. Some background info: I
I have a template for my list box item. It includes an image button.
I have a navigation bar/list that is using only HTML and CSS. The background
I need change background of all text that have two spaces from the start
i did't find any property of a tab container to change background color of
I have created a table in my application, I want to select (change background

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.