0

I am working on a project and it's nearly finished. but I've been stuck on a problem for so long now. My application uses contextual actionbar for selecting list items but as you must know, Contextual actionbar is not available for older versions of android such as gingerbread and I want to maintain compatibility for them as well as 11+. So I decided to create two layouts,One uses the actual contextual actionbar and the other uses my own custom built CAB. I started to build this project and everything was going well until now.

so I use the code below to capture the first long click and the other clicks for selecting items :

@Override
    public boolean onItemLongClick ( AdapterView < ? > adapterView , View view , int position , long id ) {

        selectedItems.add( position );

        this.listView.getChildAt( position ).setBackgroundColor( getResources( ).getColor( R.color.list_item_selected ) );
        getActivity( ).invalidateOptionsMenu( );

        return true;

    }

    @Override
    public void onListItemClick ( ListView listView , View view , int position , long id ) {

        if ( selectedItems.size( ) == 0 ) {

            getActivity( ).invalidateOptionsMenu( );

        } else {

            if ( !selectedItems.contains( position ) ) {

                selectedItems.add( position );
                view.setBackgroundColor( getResources( ).getColor( R.color.list_item_selected ) );

            } else {

                selectedItems.remove( Integer.valueOf( position ) );
                view.setBackgroundColor( getResources( ).getColor( R.color.list_item_deselected ) );

            }

            getActivity( ).invalidateOptionsMenu( );

        }

    }

and there is only one problem and that is when I select one item and I scroll down a lot of other items are selected too. now I KNOW WHAT IS CAUSING THE PROBLEM and I tried to solve it by adding these lines to my custom adapter :

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

            View listItemView = convertView;

            if ( listItemView == null ) {

                LayoutInflater layoutInflater = ( LayoutInflater ) this.context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

                listItemView = layoutInflater.inflate( id , parent , false );

                ApplicationsListViewHolder viewHolder = new ApplicationsListViewHolder( );

                viewHolder.textViewAppName = ( TextView ) listItemView.findViewById( R.id.list_item_fragment_applications_list_textview_application_name );
                viewHolder.textViewAppSize = ( TextView ) listItemView.findViewById( R.id.list_item_fragment_applications_list_textview_application_size );
                viewHolder.imageViewAppStorage = ( ImageView ) listItemView.findViewById( R.id.list_item_fragment_applications_list_imageview_application_storage );
                viewHolder.imageViewAppLogo = ( ImageView ) listItemView.findViewById( R.id.list_item_fragment_applications_list_imageview_application_icon );

                listItemView.setTag( viewHolder );

            }

            ApplicationsListViewHolder viewHolder = ( ApplicationsListViewHolder ) listItemView.getTag( );

            viewHolder.textViewAppName.setText( this.applicationManager.getAppName( this.values.get( position ) ) );
            viewHolder.textViewAppSize.setText( "Version : " + this.applicationManager.getAppVersion( this.values.get( position ) ) );
            viewHolder.imageViewAppLogo.setImageDrawable( this.applicationManager.getAppIcon( this.values.get( position ) ) );

            if ( ApplicationsListFragmentCompat.selectedItems.contains( position ) ) {

                listItemView.setBackgroundResource( R.color.list_item_selected );

            } else {

                listItemView.setBackgroundResource( R.color.list_item_deselected );

            }

            if ( this.applicationManager.checkStorage( this.values.get( position ) ) ) {

                viewHolder.imageViewAppStorage.setImageResource( R.drawable.ic_action_sd_storage );

            } else if ( this.applicationManager.checkStorage( this.values.get( position ) ) == false ) {

                viewHolder.imageViewAppStorage.setImageResource( R.drawable.ic_action_phone );

            }

            return listItemView;

        }

but since I can not recall this method every time user selects an item, it still shows the same behavior the first time but if I scroll up and down again, everything works.

I would really appreciate it if someone could give me a solution to this.

P.S. if you know a better way to achieve what I want please let me know, but I have to finish this project in 2 days at most so please suggest something not time consuming.

Thanks in advance

1 Answers1

0

Just use ActionBarCompat or ActionBarSherlock.

Community
  • 1
  • 1
FD_
  • 12,947
  • 4
  • 35
  • 62