0
                      This is the listView which is used for setting the adapter for the items.


                            ListView searchList = new (ListView) findViewById(R.id.searchResultRelativeLayout);


                /**     Here I am setting my adapter for the ListView. An inner class is used to set the adapter. Object of the inner class is passed in the adapter to fill the ListView with items.*/


                             searchList.setAdapter(new myAdAdapter(this, searchResultFirstName, searchResultLastName, searchResultStreet
                                                , searchResultLocality, searchResultMobileNumber, searchResultPostCode, searchResultDetails, interestButton));


            This is the little inner class to set the view of the ListView.


                              class myAdRow{
                                    String firstName;
                                    String lastName;
                                    String street;
                                    String locality;
                                    String mobileNumber;
                                    String postCode;
                                    String details;
                                    String button;

                                    public myAdRow(String firstName , String lastName , String street , String locality , String mobileNumber
                                    , String postCode , String details , String button){

                                        this.firstName = firstName;
                                        this.lastName = lastName;
                                        this.street = street;
                                        this.locality = locality;
                                        this.mobileNumber = mobileNumber;
                                        this.postCode = postCode;
                                        this.details = details;
                                        this.button = button;
                                    }
                                }


                                class myAdAdapter extends BaseAdapter {

                                    ArrayList<myAdRow> list;
                                    Context context;

                                    public myAdAdapter(Context context ,String [] firstName , String [] lastName , String [] street , String [] locality
                                    , String [] mobileNumber , String [] postCode , String [] details , String[] button){
                                        this.context = context;
                                        list = new ArrayList<myAdRow>();
                                        for(int i =0; i< firstName.length; i++){
                                            list.add(new myAdRow(firstName[i] , lastName[i] , street[i] , locality[i] , mobileNumber[i],
                                                    postCode[i] , details[i] , button[i]));
                                        }
                                    }


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

                                    @Override
                                    public Object getItem(int position) {
                                        return list.get(position);
                                    }

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

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


          /*  Here is the inflater which is used to inflate the view of the ListView. *The Inlating works fine and cause no problem. The only problem is with changing *the 
        *    background colour. 
        *    The on ClickListener and methods which changes the colour of the View are below.*/

                                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                        View searchResultRow = inflater.inflate(R.layout.my_search_result_row , parent , false);

                                        TextView mySearchResultRowFirstName = (TextView) searchResultRow.findViewById(R.id.mySearchResultRowFirstNameText);
                                        TextView mySearchResultRowLastName = (TextView) searchResultRow.findViewById(R.id.mySearchResultRowLastNameText);
                                        TextView mySearchResultRowStreet = (TextView) searchResultRow.findViewById(R.id.mySearchResultRowStreetText);
                                        TextView mySearchResultRowLocality = (TextView) searchResultRow.findViewById(R.id.mySearchResultRowLocalityText);
                                        TextView mySearchResultRowMobileNumber = (TextView) searchResultRow.findViewById(R.id.mySearchResultRowNumberText);
                                        TextView mySearchResultRowPostCode = (TextView) searchResultRow.findViewById(R.id.mySearchResultRowPostCodeText);
                                        TextView mySearchResultRowDetails = (TextView) searchResultRow.findViewById(R.id.mySearchResultDetailText);
                                        interestedButton = (Button) searchResultRow.findViewById(R.id.mySearchResultInterestedButton);

                            /**Here we are setting the action listeners for the button. When the button 
                            * is clicked the background colour is changed of the corrosponding view.
                            * The colour changes initially but turn to original when scrolling is done.
                            */
                                        interestedButton.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                setInterestedButtonAction(position);
                                                disableItem(position);
                                            }
                                        });

                                        myAdRow temp = list.get(position);
                                        mySearchResultRowFirstName.setText(temp.firstName);
                                        mySearchResultRowLastName.setText(temp.lastName);
                                        mySearchResultRowStreet.setText(temp.street);
                                        mySearchResultRowLocality.setText(temp.locality);
                                        mySearchResultRowMobileNumber.setText(temp.mobileNumber);
                                        mySearchResultRowPostCode.setText(temp.postCode);
                                        mySearchResultRowDetails.setText(temp.details);
                                        interestedButton.setText(temp.button);
                                        bar.setVisibility(View.GONE);
                                        return searchResultRow;
                                    }
                                }

                            /**This is the disable method where the background of the items in the list are *changed. The background is changed initially but when I scroll down and the *item 
                            goes out of view in the screen, the colour changes, what initially it was.
                            */

private void disableItem(int position){ getViewByPosition(position , searchList).setEnabled(false); getViewByPosition(position , searchList).setBackgroundColor((Color.parseColor("#e4e4e4"))); }

                            //The method below is used for getting the current view. It works fine. 
                                /**
                                 * http://stackoverflow.com/questions/24811536/android-listview-get-item-view-by-position
                                 * @param pos
                                 * @param listView
                                 * @return the view which is clicked. 

/** The stack overflow is showing error and asking for add more details, So I am writing useless code. */ */

    /**
    *This method returns the view on which the button is clicked. It returns the correct view, but ... you know the colour after scrolling.
    */
                                private View getViewByPosition(int pos, ListView listView) {
                                    final int firstListItemPosition = listView.getFirstVisiblePosition();
                                    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

                                    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
                                        return listView.getAdapter().getView(pos, null, listView);
                                    } else {
                                        final int childIndex = pos - firstListItemPosition;
                                        return listView.getChildAt(childIndex);
                                    }
                                }
Praduman Raparia
  • 121
  • 2
  • 11

1 Answers1

0

It happens because your items are being recreated. Try to set background color in getView().

Anton Malyshev
  • 8,035
  • 2
  • 23
  • 41