如何获得onClickListener的按钮来上班的Andr​​oid阵列阵列、如何获得、按钮、onClickListener

2023-09-07 09:12:29 作者:爱情弄疼了我

我已经创建了来自sqllite数据库按钮的动态数组。对于我的生活,我不能得到一个点击监听器设置单独解决每个按钮。我一直在寻找与所有没有成功3小时。

任何帮助将是AP preciated。不要担心数据库的废话,只是听众。

 进口...公共类CreatePlayList延伸活动{    滚动型scrollleft,scrollright;    的LinearLayout songsright,songsleft;    TextView的testtext;    的String [] [] allsongs;    INT numofsongs,我;    按钮B1 [];    @覆盖    保护无效的onCreate(捆绑savedInstanceState){        // TODO自动生成方法存根        super.onCreate(savedInstanceState);        的setContentView(R.layout.createplaylist);        scrollleft =(滚动型)findViewById(R.id.scrollleft);        scrollright =(滚动型)findViewById(R.id.scrollright);        songsright =(的LinearLayout)findViewById(R.id.layRight);        songsleft =(的LinearLayout)findViewById(R.id.layLeft);        LyricDb连接=新LyricDb(CreatePlayList.this);        connect.open();        numofsongs = connect.getNumSongs();        B1 =新的Button [(numofsongs)];        allsongs =新的String [numofsongs] [2];        allsongs = connect.getSongArray();        connect.close();        testtext =新的TextView(本);        testtext.setText(测试);        songsleft.addView(testtext);        对于(i = 0; I< allsongs.length;我++){            B1 [I] =新按钮(本);            B1 [I] .setText(allsongs [I] [1]);            songsright.addView(B1 [I]);        }        B1 [19] .setText(测试123);        createClic();    }    公共无效createClic(){        对于(I = 0; I&≤(allsongs.length  -  1);我++){            B1 [I] .setOnClickListener(新View.OnClickListener(){                公共无效的onClick(视图v){                    // TODO自动生成方法存根                    testtext.setText(B1 [I] .getText());                }            });        }    }} 

解决方案

转储我在类级别声明。与此(以下简称按钮theButton需要保持最终)更换方法。

 公共无效createClic(){    的for(int i = 0;我≤(allsongs.length  -  1);我++)    {        最终按钮theButton = B1 [I]        theButton.setOnClickListener(新View.OnClickListener()        {            公共无效的onClick(视图v)            {                // TODO自动生成方法存根                testtext.setText(theButton.getText());            }        });    }} 

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.

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