1

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

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

任何帮助,将不胜感激!

4

2 回答 2

1

尝试使用

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

代替

if(mPltc == "Ally")

(与“附庸”、“敌人”等相同)

您不应该==String对象上使用,因为它不会按您期望的方式工作。

于 2013-02-11T19:35:38.163 回答
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;
于 2013-02-11T19:36:02.707 回答