0

今日は響板アプリに来ました!簡単にするために、「Son」モデルのコントローラーである G_Son オブジェクトがあります。データベースからサウンドのリストを取得します(ここまではすべて問題ありません)が、ImageButtons を動的に作成してアクティビティに追加しようとすると(manageLayout())、アクティビティに何も表示されません。ボタン一つもありません。何かアイデアがある場合、または私に手を差し伸べたい場合は、どんな提案でも承知しています。

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