Search code examples
javaandroidviewcolorssimplecursoradapter

Android: Java: TextView Won't Change Color


I can't seem to figure out why my list row item won't change color:

/** 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();
}

Everything in the SimpleCursorAdapter.ViewBinder seems to be in order, but the color won't change...i suspect that it may be WHERE i placed the ViewBinder more than the ViewBinder itself.

Any help would be appreciated!


Solution

  • You can't equalize object with a string. You need to use equals or equalsIgnoreCase functions

    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;