gpt4 book ai didi

java - Android:Java:TextView 不会改变颜色

转载 作者:行者123 更新时间:2023-11-29 21:49:29 25 4
gpt4 key购买 nike

我似乎无法弄清楚为什么我的列表行项目不会改变颜色:

/** Populate the Views in act_alliances.xml with data from the database */
private void loadAllianceData() {
TblAlliances mTAlliances = new TblAlliances(this);
mTAlliances.openRead();
Cursor mCursor = mTAlliances.selectSectorData(mSector);
// load Sector Name into act_alliance_detail.xml
TextView mTxtSctName = (TextView) findViewById(R.id.allc_sname);
mTxtSctName.setText("Sector: "+mSector);
// load the "Number of Alliances" field with the count from the cursor
TextView mTxtNumAllcs = (TextView) findViewById(R.id.allc_textView2);
mTxtNumAllcs.setText(String.valueOf(mCursor.getCount()));
String[] cols = new String[] {
mTAlliances.C_FID,
mTAlliances.C_FANAME,
mTAlliances.C_FPLTC,
mTAlliances.C_FSPWER
};
int[] to = new int[] {
R.id.allc_lstRow_textView1,
R.id.allc_lstRow_textView2,
R.id.allc_lstRow_invisible,
R.id.allc_lstRow_textView3
};
// connect to the ListView and clear it just in case this isnt the first time
ListView mListView = (ListView) findViewById(R.id.allc_listView);
mListView.destroyDrawingCache();
mListView.setVisibility(ListView.INVISIBLE);
mListView.setVisibility(ListView.VISIBLE);
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
this,
R.layout.act_alliances_list_row,
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(): Political Relation: "+mPltc);
}
// Set color of item based on Political Relation
if(mPltc == "Ally"){tv.setTextColor(Color.parseColor("#6699ff"));}
if(mPltc == "Vassal"){tv.setTextColor(Color.parseColor("#00ff00"));}
if(mPltc == "Enemy"){tv.setTextColor(Color.parseColor("#ff0000"));}
return true;
}
return false;
}
});
// Assign adapter to ListView
mListView.setAdapter(dataAdapter);
mListView.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// selected item
mAllianceForDetail = ((TextView) arg1.findViewById(R.id.allc_lstRow_textView2)).getText().toString();

startAct("AllianceDetail");
}
});
mTAlliances.close();
}

SimpleCursorAdapter.ViewBinder 中的一切似乎都井井有条,但颜色不会改变...我怀疑它可能是我放置 ViewBinder 的位置而不是 ViewBinder 本身。

如有任何帮助,我们将不胜感激!

最佳答案

你不能用字符串来平衡对象。您需要使用 equalsequalsIgnoreCase 函数

if (mPltc.equalsIgnoreCase("Ally")){tv.setTextColor(Color.parseColor("#6699ff"));}
if (mPltc.equalsIgnoreCase("Vassal")){tv.setTextColor(Color.parseColor("#00ff00"));}
if (mPltc.equalsIgnoreCase("Enemy")){tv.setTextColor(Color.parseColor("#ff0000"));}
return true;

关于java - Android:Java:TextView 不会改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14819469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com