0

im trying to add the OnClickListener to the button that it generated by the value in array, but m fail to do so, any suggestion here?

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));
}
3
  • Are you adding a button to a button? linearButton.addView(btn...) Commented Jun 12, 2013 at 16:24
  • i divide my layout into 2 LinearLayout... and loop and create the button in the Linearlayout name "linearButton" Commented Jun 12, 2013 at 16:29
  • try this stackoverflow.com/questions/9049843/… Commented Jun 12, 2013 at 16:31

1 Answer 1

1

What you want to do is add the 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));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.