0

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

        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。有没有更简单的解决方案?

4

0 回答 0