这是我的应用上的联盟活动:
这是单行的布局:
<?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
然而,就目前而言,这里存在两个问题:
我需要修复这些错误,并感谢任何和所有的帮助。
注意:我没有使用ListActivity,因为前两行(Sector,Number of Alliances)不是ListView的一部分;我愿意重新考虑这个决定。
答案 0 :(得分:1)
将文本颜色设置为上一个语句中的默认值:
if(mPltc.equals("Neutral")){
tv.setTextColor(Color.parseColor("#444444"));
String mAN = cursor.getString(cursor.getColumnIndex("FANAME"));
tv.setText(mAN);
}
除此之外,我建议使代码更具可读性 - 在每个if
语句中执行相同的操作 - 将其提取到私有方法或将其移出语句。
您还可以将if
语句替换为switch-case
并使用Enums
。