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

标签 java android view colors simplecursoradapter

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

/** 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/

相关文章:

android - 带有图像和文本的按钮

entity-framework - Entity Framework 中的 View 不会更新

node.js - 在循环中向 EJS 显示 MongoDB 文档值

android - 如何获取 Recyclerview 项目上所有编辑文本的总和?仅获取屏幕上可见的项目,而不是全部

java - jQuery : post data to a jsp method?

java - 如何使用java查找操作系统驱动器?

java - Java PriorityQueue底层数据结构

java - 如何更改 Theme.AppCompat.Light.DarkActionBar 主题中菜单项的文本颜色?

android - Facebook 的 Android 应用程序(怎么做?)

java - 快捷方式 Badger 库不会在 Android Studio 中的应用程序图标上显示徽章计数