2010-09-04 111 views
1

我有一個自定義BaseAdapter的列表視圖。列表視圖的每一行都有一個TextView和一個CheckBox。顯示自定義列表視圖的默認選擇顏色

問題是當我點擊(或觸摸)任何行時,textview前景變爲灰色,而不是默認行爲(背景 - >綠色,textview前景 - >白色)。

下面是代碼:

row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       style="@style/layout"> 


    <TextView android:id="@+id/main_lv_item_textView" 
       style="@style/textViewBig" 
       android:layout_alignParentLeft="true"/> 


    <CheckBox android:id="@+id/main_lv_item_checkBox" 
       style="@style/checkBox" 
       android:layout_width="wrap_content" 
       android:layout_alignParentRight="true"/> 

</RelativeLayout> 

自定義適配器:

public class CustomAdapter extends BaseAdapter { 
     private List<Profile> profiles; 
     private LayoutInflater inflater; 
     private TextView tvName; 
     private CheckBox cbEnabled; 

     public CustomAdapter(List<Profile> profiles) { 
      this.profiles = profiles; 
      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     public int getCount() { 
      return profiles.size(); 
     } 

     public Object getItem(int position) { 
      return profiles.get(position); 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(final int position, View convertView, ViewGroup parent) { 
      View row = inflater.inflate(R.layout.main_lv_item, null); 

      final Profile profile = profiles.get(position); 
      tvName = (TextView) row.findViewById(R.id.main_lv_item_textView); 
      registerForContextMenu(tvName); 
      cbEnabled = (CheckBox) row.findViewById(R.id.main_lv_item_checkBox); 
      tvName.setText(profile.getName()); 
      if (profile.isEnabled()) { 
       cbEnabled.setChecked(true); 
      } 

      tvName.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        Bundle bundle = new Bundle(); 
        bundle.putString(PROFILE_NAME_KEY, profile.getName()); 
        Intent intent = new Intent(context, GuiProfile.class); 
        intent.putExtras(bundle); 
        startActivity(intent); 
       } 
      }); 

      tvName.setOnLongClickListener(new OnLongClickListener() { 
       public boolean onLongClick(View v) { 
        selectedProfileName = ((TextView) v).getText().toString(); 
        return false; 
       } 
      }); 

      cbEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if (!profile.isEnabled()) { 
         for (Profile profile : profiles) { 
          if (profile.isEnabled()) { 
           profile.setEnabled(false); 
           Database.getInstance().storeProfile(profile); 
          } 
         } 
        } 

        profile.setEnabled(isChecked); 
        Database.getInstance().storeProfile(profile); 
        updateListView(); 
       } 
      }); 

      return row; 
     } 
    } 

任何幫助,將不勝感激。

回答

0

你沒有張貼用來設置顏色的代碼,但是我碰到了同樣的問題,解決辦法是做一個

setBackgroundColor(0xFF000000); 

,而不是

setBackgroundColor(R.color.black); 

哪由於R.color.black不是灰色的,因此它似乎是Android中的一個bug。此外,所有設置代碼都會被調用很多次,只需要執行一次。參數convertView將在第一次爲空 - 即當你膨脹。之後,只需使用convertView。

1

實際上,發生的並不是一個錯誤,而是Android中一個不幸的輸入問題。當您將背景顏色設置爲R.color.black時,將其設置爲一個id而不是實際的顏色。由於顏色只是整數,並且ids只是整數,所以它不知道其差異,並將其解釋爲顏色。要做到這一點實際的方法是將其設置爲從資源類像這樣得到的顏色:

int black = activity.getResources().getColor(android.R.color.black); 
view.setBackgroundColor(black); 

希望這是有幫助的。