更改ListView行项的颜色

时间:2013-03-11 22:28:07

标签: android listview simplecursoradapter android-viewbinder customizing

这是我的应用上的联盟活动: ActAlliances.java

这是单行的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignBounds"
android:orientation="horizontal"
android:useDefaultMargins="true" >

<TextView
    android:id="@+id/allc_lstRow_textView1"
    android:layout_width="50sp"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:paddingRight="10dp"
    android:text="@string/tallc_id"
    android:textSize="18sp"/>
<TextView
    android:id="@+id/allc_lstRow_textView2"
    android:layout_width="140sp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/allc_lstRow_textView1"
    android:gravity="left"
    android:text="@string/lst_item_allc_name"
    android:textSize="18sp"/>
<TextView
    android:id="@+id/allc_lstRow_invisible"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/allc_lstRow_textView2"
    android:layout_width="1dip"
    android:gravity="left"
    android:textSize="1sp"/>
<TextView
    android:id="@+id/allc_lstRow_textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/allc_lstRow_invisible"
    android:layout_gravity="right"
    android:gravity="right"
    android:text="@string/lst_item_allc_power"
    android:textSize="18sp"/>
</RelativeLayout>

这是带有ViewBinder的SimpleCursorAdapter,可以自定义联盟的颜色(灰色为中性,蓝色为盟友,绿色为附庸,红色为敌人):

SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
      this, 
      R.layout.lst_row_act_allc,
      mCursor,
      cols,
      to,
      0);
    dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int column) {
            if( column == 1 ){ 
                TextView tv = (TextView) view;
                String mPltc = cursor.getString(cursor.getColumnIndex("FPLTC"));
                if (BuildConfig.DEBUG) {
                    Log.i(Constants.TAG_ACTALLIANCES, "loadAllianceData(): Alliance Name: "+cursor.getString(cursor.getColumnIndex("FANAME")));
                    Log.i(Constants.TAG_ACTALLIANCES, "loadAllianceData(): Political Relation: "+mPltc);
                }
                // Set color of item based on Political Relation
                if(mPltc.equals("Ally")){
                    tv.setTextColor(Color.parseColor("#6699ff"));
                    String mAN = cursor.getString(cursor.getColumnIndex("FANAME"));
                    tv.setText(mAN);
                }
                if(mPltc.equals("Vassal")){
                    tv.setTextColor(Color.parseColor("#00ff00"));
                    String mAN = cursor.getString(cursor.getColumnIndex("FANAME"));
                    tv.setText(mAN);
                }
                if(mPltc.equals("Enemy")){
                    tv.setTextColor(Color.parseColor("#ff0000"));
                    String mAN = cursor.getString(cursor.getColumnIndex("FANAME"));
                    tv.setText(mAN);
                }
                if(mPltc.equals("Neutral")){
                    String mAN = cursor.getString(cursor.getColumnIndex("FANAME"));
                    tv.setText(mAN);
                }
                return true;
            }
            return false;
        }
    });

现在根据Logcat,持有联盟名称的TextView与表示政治关系的数据库表中的列匹配:

03-11 21:48:15.229: I/ActAlliances(7823): loadAllianceData(): Alliance Name: IMMORTALS
03-11 21:48:15.328: I/ActAlliances(7823): loadAllianceData(): Political Relation: Neutral
03-11 21:48:15.497: I/ActAlliances(7823): loadAllianceData(): Alliance Name: Legion of Anarchy
03-11 21:48:15.497: I/ActAlliances(7823): loadAllianceData(): Political Relation: Ally
03-11 21:48:15.590: I/ActAlliances(7823): loadAllianceData(): Alliance Name: TheCulture
03-11 21:48:15.590: I/ActAlliances(7823): loadAllianceData(): Political Relation: Enemy
03-11 21:48:15.608: I/ActAlliances(7823): loadAllianceData(): Alliance Name: Rising Storm
03-11 21:48:15.608: I/ActAlliances(7823): loadAllianceData(): Political Relation: Vassal
03-11 21:48:15.647: I/ActAlliances(7823): loadAllianceData(): Alliance Name: NULL ALLIANCE
03-11 21:48:15.647: I/ActAlliances(7823): loadAllianceData(): Political Relation: Neutral
03-11 21:48:15.677: I/ActAlliances(7823): loadAllianceData(): Alliance Name: OMEGA 27
03-11 21:48:15.677: I/ActAlliances(7823): loadAllianceData(): Political Relation: Neutral

然而,就目前而言,这里存在两个问题:

  1. setViewValue()在显示ListView时以及在用户执行任何操作之前运行不少于4次。
  2. 列表中的第一项(在本例中为IMMORTALS)根据应用的最后颜色重新着色(因为灰色是列表项的默认颜色,因为代码只显示中性联盟,所以它不会真正计算名字没有涂上颜色。
  3. 我需要修复这些错误,并感谢任何和所有的帮助。

    注意:我没有使用ListActivity,因为前两行(Sector,Number of Alliances)不是ListView的一部分;我愿意重新考虑这个决定。

1 个答案:

答案 0 :(得分:1)

  1. 我在这里看不到任何问题。
  2. 将文本颜色设置为上一个语句中的默认值:

    if(mPltc.equals("Neutral")){
      tv.setTextColor(Color.parseColor("#444444"));
      String mAN = cursor.getString(cursor.getColumnIndex("FANAME"));
      tv.setText(mAN);
    }
    
  3. 除此之外,我建议使代码更具可读性 - 在每个if语句中执行相同的操作 - 将其提取到私有方法或将其移出语句。

    您还可以将if语句替换为switch-case并使用Enums