如何更改gridview中动态创建按钮的颜色

时间:2013-12-19 15:29:01

标签: java android gridview

您好我正在使用本教程来创建一个按钮描述的arraylist,允许我创建按钮。 http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/

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个按钮。似乎正在发生的事情是,无论何时点击它们,它实际上都会改变屏幕底部按钮的颜色。如果我向下滚动,它会改变屏幕底部的那个按钮的颜色。

2 个答案:

答案 0 :(得分: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);
             }
          }
        );
    }

答案 1 :(得分:0)

您是否尝试在getView中实现内联的OnClickListener?

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

我不确切知道为什么,但我有类似的问题,这有助于它。