5

i'm trying to create buttons programmatically on my android application depending on how many items I have on my sqlite database. The buttons are there, but my problem is to set onClick on every button because I want to show different content when user's click the buttons. I'm using this code for now :

   for(cursorCol.move(0); cursorCol.moveToNext(); cursorCol.isAfterLast()){
          Id = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("id")));
          Log.i("Id","Id : "+Id);
                titleButton = cursorCol.getString(cursorCol.getColumnIndex("title"));
             Log.i("titleButton","titleButton : " + titleButton);
             elemOrder1 = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("elemOrder")));
               Log.i("elemOrder1 ","elemOrder1 : " + elemOrder1 );    

               btn = new Button(this); 
                  btn.setText("  " + titleButton + "  "); 
                  btn.setId(Id);
                  btn.setTextColor(Color.parseColor("#000000"));
                  btn.setTextSize(12);
                  btn.setPadding(10, 10, 10, 10);
                  btn.setBackgroundResource(R.drawable.gray_button);
                  btnlayout.addView(btn,params); 

                  btn.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
           infoCard.removeAllViews();

           for(int i=0;i<=cursorCol.getCount();i++){

            Log.i("","titleButton : "+titleButton);

               }
          }
}

But the problem is that when I click the button it's showing only the last titleButton. Actually I don't need to show titleButton, I just did it for testing purposes. Any ideas how can I create different onClick methods for every single button?

1 Answer 1

6

I think the problem lies with this line of code:

btn = new Button(this);

You are editing the same button over and over again in your loop and not acutally creating a new one. To create a new one you will need to do this:

Button new_btn = new Button(this);

This will create a brand new one every time you iterate through your for loop.

Sign up to request clarification or add additional context in comments.

7 Comments

That's the right answer! Thanks a lot for this! I didn't realize that I'm working with the same button everytime.
He does create a new instance with "new" so that's not a problem.
He is creating a new instance and over-writing the old one he was working with. He only ever has one button, "btn". Also, he commented just above this saying my solution worked, so I think it was the problem.
Maybe I'm already tired, but I still don't see how the "btn" can cause problems: in every loop a new instance is created, stored in a field (I giess), and this new instance is configured and added to the view. So it shouldn't matter if it's stored in a field or locally. (in the code snippet he doesn't access btn outside of the for loop). For me the "titleButton" field looks suspicious, since that is actually used outside of the for loop (in the OnClick method), so my guess would be that correct button was clicked, but the wrong message was shown.
From my understanding of his code, btn is created outside of the loop and by saying it equals something else over and over overwrites it. However I would expect android to throw an error saying that the button already has a parent as it was added but there must be some other reason why that isn't happening.
|

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.