2013-02-11 51 views
1

我似乎無法弄清楚,爲什麼我的列表行項目不會改變顏色:安卓:Java的:TextView中不會變色

/** 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本身更多。

任何幫助,將不勝感激!

+0

使用等於或equalsIgnoreCase比較字符串 – Pragnani 2013-02-11 19:36:54

回答

1

你不能用一個字符串來平衡對象。您需要使用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; 
1

使用

if(mPltc.equals("Ally"))... 

,而不是

if(mPltc == "Ally") 

(同樣的, 「諸侯」, 「敵人」,等等)

你不應該使用==String對象,嘗試它不會像你所期望的那樣工作。

+1

第一:String不帶開關的工作... 下一頁:字符串不以==工作... 我寫我的國會議員(很快我發現他是誰 - 有一個應用程序)! – Quasaur 2013-02-11 21:11:20

+0

謝謝......這對顏色有效,但我注意到列表迭代不少於3次......? – Quasaur 2013-02-11 21:31:28