简体   繁体   English

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

[英]How to get onClickListener for Android array of buttons to work

I have created a dynamic array of buttons that come from sqllite database. 我创建了一个来自sqllite数据库的按钮动态数组。 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. 我一直在寻找3个小时但没有成功。

Any help would be appreciated. 任何帮助,将不胜感激。 Don't worry about the database crap, it's 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());
                }
            }); 
        } 
    }
}

Dump the 'i' declared at class level. 转储在班级声明的'i'。 Replace method with this (the "Button theButton" needs to stay 'final'). 用这个替换方法(“按钮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());
            }
        });
    }
}

First implement OnClickListener in your Activity and call setOnClickListener() method to all of your buttons when you are creating it. 首先在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
    }
    ......
}

Try it once... 尝试一次......

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM