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.

imports...

public class CreatePlayList extends Activity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createplaylist);
    scrollleft = (ScrollView) findViewById(R.id.scrollleft);
    scrollright = (ScrollView) findViewById(R.id.scrollright);
    songsright = (LinearLayout) findViewById(R.id.layRight);
    songsleft = (LinearLayout) findViewById(R.id.layLeft);
    LyricDb connect = new LyricDb(CreatePlayList.this);
    connect.open();
    numofsongs = connect.getNumSongs();
    b1 = new Button[(numofsongs)];
    allsongs = new String[numofsongs][2];
    allsongs = connect.getSongArray();
    connect.close();
    testtext = new TextView(this);
    testtext.setText("Test");
    songsleft.addView(testtext);

    for (i = 0; i < allsongs.length; i++) {
        b1[i] = new Button(this);
        b1[i].setText(allsongs[i][1]);
        songsright.addView(b1[i]);
    }

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

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
                testtext.setText(b1[i].getText());
            }
        }); 
    } 
}

}

Answers


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
                testtext.setText(theButton.getText());
            }
        });
    }
}

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 http://www.doctrine-project.org/jira/browse/DC-400

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.