How to get ClickListener for Android array of buttons to work

I have created a dynamic array of buttons that come from sqllite database. For the life of me I cannot get a click listener set up to address each button individually. I have been searching for 3 hours with no success at all.

//my code, any help would be appreciated. Don't worry about the database crap, its just the listener.


public class CreatePlayList extends Activity {

ScrollView scrollleft, scrollright;
LinearLayout songsright, songsleft;
TextView testtext;
String[][] allsongs;
int numofsongs, i;
Button b1[];

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    scrollleft = (ScrollView) findViewById(;
    scrollright = (ScrollView) findViewById(;
    songsright = (LinearLayout) findViewById(;
    songsleft = (LinearLayout) findViewById(;
    LyricDb connect = new LyricDb(CreatePlayList.this);;
    numofsongs = connect.getNumSongs();
    b1 = new Button[(numofsongs)];
    allsongs = new String[numofsongs][2];
    allsongs = connect.getSongArray();
    testtext = new TextView(this);

    for (i = 0; i < allsongs.length; i++) {
        b1[i] = new Button(this);

    b1[19].setText("test 123");

public void createClic(){
    for (i = 0; i < (allsongs.length - 1); i++) {
        b1[i].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub



Dump the 'i' declared at class level. Replace method with this (the "Button theButton" needs to stay 'final').

public void createClic()
    for (int i = 0; i < (allsongs.length - 1); i++)
        final Button theButton = b1[i];
        theButton.setOnClickListener(new View.OnClickListener()

            public void onClick(View v)
                // TODO Auto-generated method stub

Need Your Help

PHP - Doctrine ORM not able to handle bit(1) types correctly?

php mysql types doctrine

UPDATE I have filed a bug in Doctrine about this

How to use properties in a Spring project to configure log4j.xml

spring logging log4j

I have multiple properties files in my Spring project. The spring context loads these properties and handles property overriding in a convenient manner. Is there a way to take the properties that...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.