如何使onClickListener for Android数组按钮工作

时间:2011-11-05 04:33:00

标签: java android arrays dynamic android-button

我创建了一个来自sqllite数据库的动态按钮数组。对于我的生活,我无法设置点击监听器来单独处理每个按钮。我一直在寻找3个小时但没有成功。

任何帮助将不胜感激。不要担心数据库废话,它只是听众。

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());
                }
            }); 
        } 
    }
}

3 个答案:

答案 0 :(得分:2)

转储在班级声明的'i'。用这个替换方法(“按钮theButton”需要保持'最终')。

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());
            }
        });
    }
}

答案 1 :(得分:1)

首先在Activity中实现OnClickListener,并在创建时为所有按钮调用setOnClickListener()方法。

b[i].setOnClickListener(this);
and also set id with each button 
b[i].setId(i);  

//and override this method
@Override
public void onClick(View v) 
{
    //here check which button called
    if(v.getId==1)
    {
        //its Button 1  do what ever u want     
    }
    if(v.getId==2)
    {
        //its Button 2  do what ever u want its Button 2
    }
    ......
}

答案 2 :(得分:0)

尝试一次......

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

        b1[i].setText(allsongs[i][1]);
        songsright.addView(b1[i]);
    }