1

I am actually facing a problem that I dont know how to solve...

In fact, I want to change color and display a picture on a listView when I click on it.

The problem is when I click my first item, the 0th , 9th, 18th etc... change too... it's just like the View onItemClick takes only the visible part of the listview (the displayed item, without scroll)

I dont know how to solve it.

My adapter :

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

        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {

            convertView = mInflater
                    .inflate(R.layout.item_anomalies, null);

            holder = new ViewHolder();

            holder.txtTitre = (TextView) convertView
                    .findViewById(R.id.titre);

            holder.img = (ImageView) convertView
                    .findViewById(R.id.img);

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

        final AnomalieRowItem rowItem = (AnomalieRowItem) getItem(position);

        holder.txtTitre.setText(rowItem.getLibelle());

        if (position % 5 == 0) {
            holder.img.setBackgroundColor(0xffED695A);
        } else {
            if (position % 4 == 0) {
                holder.img.setBackgroundColor(0xffEB5443);
            } else {
                if (position % 3 == 0) {
                    holder.img.setBackgroundColor(0xffEA4735);
                } else {
                    if (position % 2 == 0) {
                        holder.img.setBackgroundColor(0xffE93825);
                    } else {
                        holder.img.setBackgroundColor(0xffD52815);
                    }
                }
            }
        }

        return convertView;
    }

My listener :

 private void initListener() {
        m_ListeAnomalie.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                AnomalieRowItem item = m_ListeItem.get(position);

                ImageView imgCheck = (ImageView) view.findViewById(R.id.check);
                LinearLayout layout = (LinearLayout) view.findViewById(R.id.item);

                if (item.isChecked()) {
                    layout.setBackgroundColor(0xffefefef);
                    item.setChecked(false);
                    imgCheck.setVisibility(View.GONE);
                } else {
                    layout.setBackgroundColor(0xffdddddd);
                    item.setChecked(true);
                    imgCheck.setVisibility(View.VISIBLE);
                }

                m_Adapter.notifyDataSetChanged();
            }
        });

        m_Valider.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                end();
            }
        });
    }

Thanks for your help

EDIT : The part with the modulo and the background works fine, my problem is in the listener, it's like it's not the correct view that is injected in the OnItemClick function

EDIT 2 : With a picture, it's better I think

Android problem when OnItemClick

Anonymous
  • 11,748
  • 6
  • 35
  • 57
mfrachet
  • 8,772
  • 17
  • 55
  • 110
  • Can you please elaborate more? i didn't get it what is your problem and what you want to achieve. – Bhavesh Jethani Jun 19 '14 at 06:23
  • I have editted my post – mfrachet Jun 19 '14 at 06:28
  • I have check your code .. In that you have use ImageView instead of Checkbox. 2nd thing onItemClick you are getting item Class . item.isChecked() in that you will get the old value not the new one. – Bhavesh Jethani Jun 19 '14 at 06:39
  • You can implement code inside getView(). checkBox's onClick method. – Bhavesh Jethani Jun 19 '14 at 06:39
  • either, inside onItemClicked() event you have to get child view using view and then convert it into checkbox and check is true or not.. – Bhavesh Jethani Jun 19 '14 at 06:41
  • I dont want to use a checkbox. I just want to display an image at the end of the line. – mfrachet Jun 19 '14 at 06:43
  • So only use my rowItem value to change background color is not a good idea ? – mfrachet Jun 19 '14 at 07:01
  • its not only use its one of the use of rowItem. – Bhavesh Jethani Jun 19 '14 at 08:16
  • You don't show the implementation of `getItem()` Further we cannot see the connection between `m_ListeItem` and `getItem()`. And... All changes to the gui should be done in `getView()`. So there you have to use `isChecked()`. Try once without `notifyDatasetChanged`. And remove -temporarily- all `setBackgroundColor` in `getView`. – greenapps Jun 19 '14 at 09:14

2 Answers2

1
if (position % 5 == 0) {
            holder.img.setBackgroundColor(0xffED695A);
        } else {
            if (position % 4 == 0) {
                holder.img.setBackgroundColor(0xffEB5443);
            } else {
                if (position % 3 == 0) {
                    holder.img.setBackgroundColor(0xffEA4735);
                } else {
                    if (position % 2 == 0) {
                        holder.img.setBackgroundColor(0xffE93825);
                    } else {
                        holder.img.setBackgroundColor(0xffD52815);
                    }
                }
            }
        }
// add this lines in getView() after above logic.
if (rowItem.isChecked()) {
                    layout.setBackgroundColor(0xffefefef);
                    imgCheck.setVisibility(View.GONE);
                } else {
                    layout.setBackgroundColor(0xffdddddd);
                    imgCheck.setVisibility(View.VISIBLE);
                }
Bhavesh Jethani
  • 3,891
  • 4
  • 24
  • 42
0

You are not using rowItem.isChecked() in getView.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Because I dont need it :o. I m using it on the activity, where I change my background color. Is it correct to put listeners in the adapter ? – mfrachet Jun 19 '14 at 06:10