change color of one specific row in listview

803 views Asked by At

I have a listview (liste_joueurs), when i click on an item in the list, the row change color and become grey ( this is for the user, he can remenber on which row he click ).

It's not finish i have 12 button and each item on a list got a number, when i click on an item in the list one button appears and his text take the number of the player, here's a picture of the fragment.

enter image description here

If the user make a mistake, I have a onclickListener for the 12 button below and when I click on, the button disappears, but my problem is I need to find the good row corresponding to the number and change his color in white. How can i do this ?

Now let's see the code

In my fragment :

 // I take all the player in my database
 Cursor cursor = joueurDab.getAll();
 //I fill the listView with a cursorAdapter
 CursorListJoueur cl = new CursorListJoueur(context, cursor);
 liste_joueurs.setAdapter(cl);

The cursorAdapter :

public class CursorListJoueur extends CursorAdapter {

private CategorieDAO categorieDab;
public CursorListJoueur(Context pContext, Cursor c) {
    super(pContext, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    categorieDab = new CategorieDAO(context);
    return LayoutInflater.from(context).inflate(R.layout.row_player, parent, false);
//row_player is the xml file where I highlight row 
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    Categorie categorie = this.categorieDab.selectionner(cursor.getInt( 6 ));

    ((TextView) view.findViewById(R.id.numero)).setText(String.valueOf(cursor.getInt(1)));
    ((TextView)view.findViewById( R.id.nom )).setText(cursor.getString(2));
    ((TextView)view.findViewById( R.id.prenom )).setText(cursor.getString( 3 ));
    ((TextView)view.findViewById( R.id.categorie )).setText(categorie.getNom());


    if( cursor.getInt( 5 ) == 1)
        ((CheckBox)view.findViewById( R.id.is_goal )).setChecked( true );
    else
        ((CheckBox)view.findViewById( R.id.is_goal )).setChecked( false );
}
}

The row_player.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
android:layout_width="match_parent"
//this ligne
android:background="@drawable/testlist"
android:layout_height="match_parent">

<TextView
    //etc ...

And finally testlist.xml i use this file for highlight my listview

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/darker_gray" android:state_selected="true"/>
    <item android:drawable="@android:color/darker_gray" android:state_activated="true"/>
    <item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
    <item android:drawable="@color/android:transparent"/>
</selector>

Thanks for your help

1

There are 1 answers

1
Erik Duisters On BEST ANSWER

You have to set the background of the row you want highlighted via your adapter. If the user presses the wrong button change your adapter data (or tell it which row you want highlighted) and call notifyDataSetChanged().

The adapter can than set the row's background color to white in it's getView() function.