我在哪里编码动态创建的android按钮,以及如何将值传递给侦听器

时间:2013-10-04 01:47:47

标签: android button android-intent dynamic onclicklistener

所以我正在创建动态分配的按钮。我想查询我的数据库,并根据用户选择的表(动态按钮)打开一个新活动,用户可以使用指定的表。

        protected void createButtons(){
    // dynamically created buttons work just need to
    LinearLayout scrViewButLay = (LinearLayout) findViewById(R.id.scrollViewLinLay);
    scrViewButLay.removeAllViews();
    ArrayList<String> tanks = new ArrayList<String>();
    tanks = dbHand.getTableList();
    Button[] tankButtons = new Button[tanks.size()];
    Log.i("DB_Tag", "DB Size = " + tanks.size());
    for(int i = 0; i < tanks.size(); i++){
        Log.i("DB_NAME_TAG", tanks.get(i));
    }
    for (int index = 0; index < tanks.size(); index++) {
        tankButtons[index] = new Button(this); 
        tankButtons[index].setText(tanks.get(index));
        scrViewButLay.addView(tankButtons[index]);
        tankButtons[index].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Each buttons specific event
                /*
                 * Left off here
                 */
                Intent intent = new Intent(MainAquariumActivity.this, ProfilesOptionsScreen.class);
                startActivity(intent);
                //How would I send tanks.get(index) to the new ProfilesOptionsScreen
            }
        });
    }

    // end of dynamically allocated buttons
}

我假设您必须动态创建onClickListeners以及创建按钮。有没有更简单的解决方案呢?

0 个答案:

没有答案