【问题标题】:change color of one specific row in listview更改列表视图中某一特定行的颜色
【发布时间】:2015-06-08 14:21:02
【问题描述】:

我有一个列表视图(liste_joueurs),当我点击列表中的一个项目时,该行会改变颜色并变为灰色(这是给用户的,他可以记住他点击了哪一行)。

这还没有完成,我有 12 个按钮,列表中的每个项目都有一个数字,当我单击列表中的一个项目时,会出现一个按钮,并且他的文本会占用玩家的编号,这是片段的图片。

如果用户弄错了,我有一个 onclickListener 用于下面的 12 按钮,当我点击时,按钮消失,但我的问题是我需要找到与数字对应的好行并将其颜色更改为白色.我该怎么做?

现在让我们看看代码

在我的片段中:

 // 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);

游标适配器:

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

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 ...

最后 testlist.xml 我使用这个文件来突出显示我的列表视图

<?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>

感谢您的帮助

【问题讨论】:

    标签: android xml listview android-listview android-cursoradapter


    【解决方案1】:

    您必须通过适配器设置要突出显示的行的背景。如果用户按下了错误的按钮,则更改您的适配器数据(或告诉它您要突出显示哪一行)并调用 notifyDataSetChanged()。

    适配器可以在其 getView() 函数中将行的背景颜色设置为白色。

    【讨论】:

    • 好的,所以我需要在 cursorAdapter 中创建 getView() 函数,然后在哪里以及如何使用它?我很难理解 getView 的工作原理,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多