Android Program Button

I am trying to create buttons programmatically in an Android application depending on the number of elements that I have in my sqlite database. There are buttons, but my problem is to set onClickeach button, because I want to display different content when the user clicks the buttons. I am using this code:

   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 press the button, it shows only the last one titleButton. In fact, I do not need to show titleButton, I just did it for testing. Any ideas how I can create different methods onClickfor each individual button?

+5
source share
1 answer

, :

btn = new Button(this);

. , :

Button new_btn = new Button(this);

, for.

+6

All Articles