Search code examples
androidlistviewcustom-adapter

ListView Highlight selected items


I can't highlight all the selected items. Only one item is highlighted at a time. If next item is clicked the previous highlighted item goes back to normal.

Here is my CustomAdapter

private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {

        public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {

            //let android do the initializing :)
            super(context, textViewResourceId, Strings);
        }

        public void setSelectedIndex(int ind)
        {
            selectedIndex = ind;
            notifyDataSetChanged();
        }

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

        @Override
        public HashMap<String, Object> getItem(int position)
        {
            return dataList.get(position);
        }

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

        //class for caching the views in a row
        private class ViewHolder {

            TextView not, isRead;
            LinearLayout noti_linear;
        }

        //Initialise
        ViewHolder viewHolder;

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

            if (convertView == null) {

                //inflate the custom layout
                convertView = inflater.from(parent.getContext()).inflate(R.layout.notification_layout, parent, false);
                viewHolder = new ViewHolder();

                //cache the views
                viewHolder.noti_linear = (LinearLayout) convertView.findViewById(R.id.not_layout);
                viewHolder.not = (TextView) convertView.findViewById(R.id.textview_noti);
                viewHolder.isRead = (TextView) convertView.findViewById(R.id.textview_isRead);

                //link the cached views to the convertview
                convertView.setTag(viewHolder);
            } else
                viewHolder = (ViewHolder) convertView.getTag();

            //set the data to be displayed
            viewHolder.not.setText(dataList.get(position).get("CheckList").toString());

            if(selectedIndex!= -1 && position == selectedIndex)
            {
                viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT);
            }
            else
            {
                viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY);
            }


//            if (getItemId(position) == StringHolder.mSelectedItem) {
//                viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY);
//
//            } else {
//                viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT);
//            }

            return convertView;
        }
    }

Here is my onItemClick

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                cardAdapter.setSelectedIndex(position);
            }
        });

Solution

  • You can do it by below code instead of applying in adapter class, Check below code,

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
                //cardAdapter.setSelectedIndex(position);
            }
        });