如何重用方法向linearlayout添加视图

时间:2014-08-05 21:29:25

标签: android

我有一项活动,其中linearlayouts动态添加到基本线性布局中。 您可以从我的代码中了解,但我尝试重新使用相同的" addBlock"在活动中添加更多数据的方法。如果你能帮我解决这个问题,我将不胜感激!谢谢!

我的代码:

private void addData01() {
        wii=R.drawable.img01;
        d=getString(R.string.one_d); //this is later set to a seperate textview
        //START OF BLOCK 1//
        b1_t = getString(R.string.Title1);
        b1 = new String[]{"DATA WILL GO HERE"};
        b1_i = new String[]{"DATA WILL GO HERE"};
        addBlock();
        //START OF BLOCK 2//
        b1_t = getString(R.string.Title2);
        b1 = new String[]{"MORE DATA WILL GO HERE"};
        b1_i= new String[]{"MORE DATA WILL GO HERE"};
        addBlock();
    }

    private void addBlock() {

        LayoutInflater inflater = LayoutInflater.from(this);
        for(int i = 0 ; i < b1.length ; i++)
        {
            View childView = inflater.inflate(R.layout.two_row, null,false);       

            TextView tv = (TextView)childView.findViewById(R.id.one);
            TextView tv2 = (TextView)childView.findViewById(R.id.two);
            tv.setText(Html.fromHtml(b1[i]));
            tv2.setText(Html.fromHtml(b1_i[i]));
            b_ll.addView(b1t);
            b_ll.addView(childView);
        }       
    }

然而,当我这样做时,我收到以下错误:

08-05 21:18:15.462: E/AndroidRuntime(4241): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我试过看How to add same view to parent multiple times by inflating it only once,但那里的解决方案似乎不起作用

0 个答案:

没有答案