从数据库中动态添加ImageButton不显示

时间:2015-01-25 13:01:59

标签: android dynamic-programming android-imagebutton

我今天在这里为我的音板应用程序!儿子要简单,我有一个G_Son对象,它是“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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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