Android 使用Cursoradapter和bindView更改列表视图的行颜色

Android 使用Cursoradapter和bindView更改列表视图的行颜色,android,listview,android-listview,android-cursoradapter,Android,Listview,Android Listview,Android Cursoradapter,我遇到了一些麻烦,当我点击我的Listview时,我想更改所选行的颜色,但是几行更改了颜色,而不是我想要的颜色 如果我单击另一行,我希望前一行保持其颜色 还有一个小问题,因为我每次都调用setAdapter,所以每次单击时列表都会向上滚动 片段中onclicklistener的代码: private AdapterView.OnItemClickListener ajouter_joueur_liste_listener = new AdapterView.OnItemClickListener

我遇到了一些麻烦,当我点击我的Listview时,我想更改所选行的颜色,但是几行更改了颜色,而不是我想要的颜色

如果我单击另一行,我希望前一行保持其颜色

还有一个小问题,因为我每次都调用setAdapter,所以每次单击时列表都会向上滚动

片段中onclicklistener的代码:

private AdapterView.OnItemClickListener ajouter_joueur_liste_listener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View v, int position, long id ) {
        //I take all player in my database 
        Cursor cursor = joueurDab.getAll();
        CursorListJoueur cl = new CursorListJoueur(context, cursor, position, true);
        liste_joueurs.setAdapter(cl);
     }
}
光标适配器

public class CursorListJoueur extends CursorAdapter {
        int poscolor;
        boolean selection;
        private CategorieDAO categorieDab;
        public CursorListJoueur(Context pContext, Cursor c, int poscolor, boolean selection) {
            super(pContext, c, 0);
            this.poscolor = poscolor;
            this.selection = selection;
        }        

        @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);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            int pos = cursor.getPosition();
            if ( poscolor != -1 ){
                if ( cursor.getPosition() == poscolor && selection ) {
                    view.setBackgroundColor( Color.GRAY );
                }else if ( cursor.getPosition() == poscolor && !selection ){
                    view.setBackgroundColor( Color.WHITE );
                }
            }
在CursorListJouer中(上下文、光标、int-poscolor、布尔选择)

参数poscolor对应于我要着色的行,布尔值指示我是要设置灰色还是白色,您不需要关心if(poscolor!=-1)

我看到了其他一些主题,但我不想对颜色行使用XML方法(因为我想在另一个onclicklistener中删除行颜色),如果可能的话,我想保留bindview()而不使用getView()


感谢您

而不是每次单击列表时都创建一个新的
适配器
(这会导致列表滚动回顶部),您可以利用SparseBooleanArray跟踪当前正在选择的项目

在适配器中:

public class YourAdapter extends CursorAdapter {

   // Initialize the array
   SparseBooleanArray selectionArray = new SparseBooleanArray();

   ...

   // Method to mark items in selection
   public void setSelected(int position, boolean isSelected) {
       selectionArray.put(position, isSelected);
   }

   ...

   @Override
   public void bindView(View view, Context context, Cursor cursor) {
       int position = cursor.getPosition();
       boolean isSelected = selectionArray.get(position);
       if (isSelected ) {
           view.setBackgroundColor( Color.GRAY );
       } else if (!isSelected){
           view.setBackgroundColor( Color.WHITE );
       }
   }
}
单击项目后,您可以同样切换选择:

@Override
public void onItemClick(AdapterView parent, View v, int position, long id ) {
    YourAdapter adapter = (YourAdapter) parent.getAdapter();
    adapter.setSelected(position, true);
    adapter.notifyDataSetChanged();
}
希望这有帮助。:)