0

我试图将 OnClickListener 添加到它由数组中的值生成的按钮中,但我没有这样做,这里有什么建议吗?

long lprice = Long.parseLong(searchId.getText().toString());
List<String> buttonNameOne = dbc.getItemNameRbPrice(lprice);
List<String> buttonPriceOne = dbc.getPriceRbPrice(lprice);
List<String> buttonDateOne = dbc.getCurrentDateRbPrice(lprice);

for(int i = 0 ; i < buttonNameOne.size() ;  i ++)
{
  Button btn = new Button(this);
  btn.setId(2000+i);
  btn.setText(buttonNameOne.get(i) + i);
  linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  Log.v("Value of element "+i, buttonNameOne.get(i));
  Log.v("Value of element "+i, buttonPriceOne.get(i));
  Log.v("Value of element "+i, buttonDateOne.get(i));
}
4

1 回答 1

1

你想要做的是添加 btn.setOnClickListener

for(int i = 0; i < buttonnameOne.size(); i++)
{
    Button btn = new Button(this);
    btn.setId(2000+i);
    btn.setText(buttonNameOne.get(i) + i));
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(view v) {
            // TODO Whatever you want onclick to do
        }
    });
    //Log stuff
    linearButton.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
于 2013-06-12T17:46:27.180 回答