这是我的应用程序上的联盟活动:
这是单行的布局:
<?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
然而,就目前而言,这里有两个问题:
- setViewValue() 在 ListView 显示时和用户执行任何操作之前运行不少于 4 次。
- 列表中的第一项(在本例中为 IMMORTALS)根据最后应用的颜色重新着色(因为灰色是列表项的默认颜色,它实际上并不算数,因为代码只显示中立联盟名称而不应用一种颜色。
我需要修复这些错误,并希望得到任何帮助。
注意:我没有使用 ListActivity,因为前两行(Sector、Number of Alliances)不是 ListView 的一部分;我愿意重新考虑这个决定。