vista de lista de Android: el primer elemento se resalta automáticamente y no puedo desmarcar el primer elemento de la vista de lista

i want multiple items to be selected,and highlighted ,this part works as expected but the problem is the first item also gets highlighted and i don't want it to happen .

The first item gets highlighted automatically and i am unable to un-highlight it even though i can highlight and un-highlight the rest of the items

here is how i am trying to highlight

    SngList.setOnItemClickListener(new OnItemClickListener(){

                                    @Override
                                    public void onItemClick(

                                            AdapterView<?> parent, View view,
                                            int position, long id) {

                                        highlight();                                            
                                    }

                                    private void highlight() {

//                                      if      (SngList.isItemChecked(0))
//                                      {   SngList.setItemChecked(position, false);
//                                      ab.notifyDataSetChanged();
//                                      
//                                      }
//                                      
                                        {   SngList.setItemChecked(position, true);
                                        ab.notifyDataSetChanged();
                                        return;
                                        }
                                        // else if (SngList.isItemChecked(position))

                                            //{SngList.setItemChecked(position, false );
                                            //ab.notifyDataSetChanged();
                                            //}

                                    }});

and then in the getview method i am changing the color by

 if(list.isItemChecked(position)){
           v.setBackgroundColor(Color.parseColor("#239700"));
        }
        else{
              v.setBackgroundColor(Color.TRANSPARENT);
        }

preguntado el 28 de mayo de 14 a las 13:05

¿Qué es lo que quieres hacer? -

as described by my question,there are unusual things happening which i don't want -

What is that thing? Elaborate more -

use a tempPositon variable to store the position of last clicked item. And try calling the obtener vista method twice with the recently clicked item position and tempPositon one after another inside same method. -

see here is the thing,i want multiple items to be selected,and highlighted ,this part works as expected but the problem is the first item also gets highlighted and i don't want it to happen -

1 Respuestas

As far as I can see, you are not unchecking the previously clicked item. Keep of track of it with an int so that you can remove it when another item is selected.

contestado el 28 de mayo de 14 a las 13:05

okay.....your answer doesn't explain why the rest of items can be un-selected or un-highlighted but the problem arises with only the first item secondly why does the first item gets automatically highlighted ?? - Ankit Srivastava

Sorry, I posted before you clarified that you were highlighting multiple views. Android recycles views in the ListView, this is probably the reason it is being highlighted each time - Woodstown

i think android only recycles the views when you specify it,and that's called view re usability which i am implementing by a view holder pattern and i don't think how this thing would cause only the first item to be highlighted automatically ? - Ankit Srivastava

You need to post more of your code then, the first item must be getting checked automatically somewhere - Woodstown

What about your custom adapter? - Woodstown

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.