0

I'm here today for my soundboard app ! son to make it simple i have a G_Son object which is the controller of the "Son" model. i get the list of sound from my database (everything is fine until here) but then when I dynamically try to create ImageButtons and add them on my activity (manageLayout() ), I have absolutely nothing appearing on my activity ! not even a single button. So if you have any Idea, or want to give me a hand, I'm aware of any suggestion

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);
        }
    }
}
4

1 回答 1

0

忘了提到尺寸,此外,在学习了几个月的android之后,我应该建议人们使用网格视图来这样做,它允许使用自定义布局来做事件,而不是使用动态图像按钮创建自己的布局

于 2015-07-02T14:26:20.727 回答