从未显示的数据库动态添加 ImageButton

Add ImageButton dynamically from Database not displayed

我今天来这里是为了我的音板应用程序!为简单起见,我有一个 G_Son 对象,它是 "Son" 模型的控制器。我从我的数据库中获取了声音列表(直到这里一切都很好)但是当我动态地尝试创建 ImageButtons 并将它们添加到我的 activity (manageLayout() ) 时,我的 activity!甚至没有一个按钮。所以如果你有任何想法,或者想帮助我,我知道任何建议

private G_Son gson;
private OurNiceSoundPlayer soundPlayer;
private List<Son> sons;
RelativeLayout gameBoard;
private Son selectedSound;
private View.OnClickListener mSoundOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        soundPlayer.setSound(sons.get(Integer.parseInt(v.getTag().toString())));
        Log.i("Board", "Clicked on ImgButton ->" + v.getTag());
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_board);

    gson = G_Son.getInstance();
    sons = gson.getSons(getApplicationContext());

    soundPlayer = new OurNiceSoundPlayer(getApplicationContext());

    gameBoard = (RelativeLayout) findViewById(R.id.soundboard);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
    manageLayout();

    gameBoard.invalidate();
}


private void manageLayout() {

    if (sons.size()>0)
    {
        int rawNbr = (int) Math.ceil((double) sons.size() / 3);
        int currentSon = 0;
        Son displayed = sons.get(currentSon);

        for (int i = 0; i < rawNbr; i++)
        {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            for (int j = 0; j < 3; j++)
            {
                ImageButton btnTag = new ImageButton(this);
                if (displayed.getIsPerso()) {
                    File imgFile = new File(displayed.getPathImage());

                    if (imgFile.exists()) {

                        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                        btnTag.setImageBitmap(myBitmap);
                    } else {
                        btnTag.setImageResource(R.drawable.defaultimage);
                    }
                }
                else
                {
                    int id = getResources().getIdentifier("com.example.m.sbst:drawable/" + displayed.getPathImage(), null, null);
                    btnTag.setImageResource(id);
                }
                btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                btnTag.setOnClickListener(mSoundOnClickListener);
                btnTag.setBackgroundColor(Color.TRANSPARENT);
                btnTag.setTag(currentSon);
                btnTag.setId(i);
                row.addView(btnTag);

                currentSon++;
                if (currentSon>=sons.size())
                {
                    break;
                }
                else
                {
                    displayed = sons.get(currentSon);
                }
            }

            gameBoard.addView(row);
        }
    }
}

忘了说尺寸,而且,经过几个月的学习 android,我应该建议人们使用网格视图来这样做,它允许使用自定义布局来做事件比创建自己的布局更好带动态图像按钮