2013-12-19 151 views
0

您好我正在使用本教程,以創建一個按鈕描述的數組列表,它允許我創建按鈕。 http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/如何更改GridView中動態創建的按鈕的顏色

public class CategoryButtonAdapter extends BaseAdapter{ 
private Context mContext; 
private ArrayList<DishCategory> dishCategories; 
//button to be created 
private Button button; 
//will take in an array list created in the orderlayout that will be the 
//dish category. This will be the from where we will the count for the adapter 
public CategoryButtonAdapter(ArrayList<DishCategory> dishCategories) 
{ 
    this.dishCategories = dishCategories; 
} 
public CategoryButtonAdapter(Context context, ArrayList<DishCategory> dishCategories) 
{ 
    this.mContext = context; 
    this.dishCategories = dishCategories; 
} 

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

//to be implementated later so it can b3e used to find menu categories 
public Object getItem(int position) 
{ 
    return null; 
} 

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

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    //button to be created 
    if(convertView == null) 
    { 
     //if it is not recycled, initialize some new attributes 
     button = new Button(this.mContext); 
     button.setLayoutParams(new GridView.LayoutParams(100,100)); 
     button.setPadding(2,2,2,2); 
    } 
    else 
    { 
     button = (Button) convertView; 
    } 
    //setButton to the description of the category 
    button.setText(dishCategories.get(position).getDescription()); 

    //this can be changed later to change the sex appeal of the app 
    //for now it will be plain 
    button.setTextColor(Color.WHITE); 
    button.setBackgroundColor(Color.BLACK); 
    button.setHighlightColor (Color.GREEN); 
    button.setId(position); 
    button.setOnClickListener(new DishCategoryButtonListener(button.getId())); 
    //new loadDishItems(categoryButtons.get(position).getDescription())); 
    return button; 
} 

現在能夠創建按鈕爲希望在時的主要活動叫。 這裏是我的自定義onclicklistener 類

DishCategoryButtonListener implements OnClickListener 
    { 
     private final int position; 

     private DishCategoryButtonListener(int position) { 
      this.position = position; 
     } 

     public void onClick(View v) 
     { 

      System.out.println("The position is " + position); 

      //button.setTextColor(Color.BLACK); 
      //button.setBackgroundColor(Color.GREEN); 
     } 

我會碰到的問題是,每當我選擇了屏幕上的按鈕。它會向我顯示正確的位置,但出於某種原因,如果選擇它,它不會將顏色更改爲綠色和黑色。如果選擇,我希望它是綠色的,如果不是,則是黑色。有沒有辦法做到這一點?我是否必須在主類中實現onclick監聽器,還是必須使用此自定義監聽器?

* 編輯 *我只是試過,而我所看到的是一種模式。比方說,列表包含數組列表中的大約20個對象。這然後在1列gridview中創建20個按鈕。似乎正在發生的事情是,每當點擊它們時,它實際上會改變屏幕底部按鈕的顏色。如果向下滾動,它將更改屏幕底部該按鈕的顏色。

回答

0

您是否嘗試過在getView中嵌入OnClickListener?

button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View button) { 
     button.setTextColor(Color.BLACK); 
     button.setBackgroundColor(Color.GREEN); 
    } 
} 

我不知道到底是爲什麼,但我有類似的問題,這有助於。

+0

我只是試過,而我所看到的是一種模式。比方說,列表包含數組列表中的大約20個對象。這然後在1列gridview中創建20個按鈕。似乎正在發生的事情是,每當點擊它們時,它實際上會改變屏幕底部按鈕的顏色。如果向下滾動,它將更改屏幕底部該按鈕的顏色。 – Jesusrz001

+0

哦,我只是意識到你不使用Holder模式!這很容易實現,對於您的列表性能非常重要。請在這裏閱讀:http://www.jmanzano.es/blog/?p=166 現在會發生什麼:在創建20個項目的過程中,button變量總是被當前創建的列表項覆蓋。這就是爲什麼你的底部項目總是被改變的原因:這是最後創建的。 – muetzenflo

0

把代碼放到事件調度線程 - 最容易在這種情況下可能是使用SwingUtilities.invokeLater()。

public void onClick(View v) 
    { 

     System.out.println("The position is " + position); 

     SwingUtilities.invokeLater 
     (
      new Runnable() 
      { 
      public void run() 
      { 
       button.setTextColor(Color.BLACK); 
       button.setBackgroundColor(Color.GREEN); 
      } 
      } 
     ); 
    } 
+0

哎呀,錯過了這是Android。我相信他們有類似的東西... – arcy

+0

是的..謝謝。 – Jesusrz001