2013-12-19 112 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