0

我正在制作一个可以离线使用的在线商店应用程序;因此数据库的东西和很多其他的东西。

我在主 xml 文件 (home_screen_categories_products_linear_layout) 中有一个 LinearLayout,我对其进行膨胀并从另一个文件 (product_details_layout) 添加布局。在这第二个布局(代表产品详细信息)中,我有另一个 LinearLayout,其中包含客户对产品的意见(opinionsList),我再次从另一个 xml 文件(opinion_layout)膨胀。

当然,我得到“指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。” 例外。

我以前遇到过这种情况,但设法解决了这个问题;但是在这种情况下,我想不出该怎么做。

我期待这个例外,但尝试了它希望它会起作用;我在这里阅读了另一个类似的问题,但没有找到解决方案。基本上你如何在另一个布局中膨胀一个已经膨胀到另一个布局中的布局?(如果那有意义的话)

下面是显示产品详细信息的方法代码:

    LinearLayout parentLayout = (LinearLayout) findViewById(R.id.home_screen_categories_products_linear_layout);
    parentLayout.removeAllViews();
    View view = layoutInflater.inflate(R.layout.product_details_layout, parentLayout, false);

    TextView pretNou = (TextView) view.findViewById(R.id.ProductDetailsNewPrice);

    ..........................

    String userName = c.getString(c.getColumnIndex(KEY_P_USER_NAME));
    String userRating = c.getString(c.getColumnIndex(KEY_P_USER_RATING));
    String comments = c.getString(c.getColumnIndex(KEY_P_COMMENT));
    String commentDate = c.getString(c.getColumnIndex(KEY_P_DATE));

    LinearLayout opinionsParentLayout = (LinearLayout) view.findViewById(R.id.opinionsList);
    opinionsParentLayout.removeAllViews();
    View opinionView = layoutInflater.inflate(R.layout.opinion_layout, parentLayout, false);

    if(userName.length()!= 0)
    {
        StringTokenizer nameSplitter = new StringTokenizer(userName,"{|}");
        StringTokenizer userRatingSplitter = new StringTokenizer(userRating,"{|}");
        StringTokenizer commentsSplitter = new StringTokenizer(comments,"{|}");
        StringTokenizer commentDateSplitter = new StringTokenizer(commentDate,"{|}");   

        while (nameSplitter.countTokens() >0 )
        {

            TextView user = (TextView) opinionView.findViewById(R.id.commentUser);
            user.setText(nameSplitter.nextToken() + " · " + commentDateSplitter + " Rating:");

            ImageView userRatingIV = (ImageView) opinionView.findViewById(R.id.userRating);
            switch ( Integer.parseInt(userRatingSplitter.nextToken()) )
            {
                case 0 : userRatingIV.setImageDrawable(getResources().getDrawable(R.drawable.stars0)); break;
                case 1 : userRatingIV.setImageDrawable(getResources().getDrawable(R.drawable.stars1)); break;
                case 2 : userRatingIV.setImageDrawable(getResources().getDrawable(R.drawable.stars2)); break;
                case 3 : userRatingIV.setImageDrawable(getResources().getDrawable(R.drawable.stars3)); break;
                case 4 : userRatingIV.setImageDrawable(getResources().getDrawable(R.drawable.stars4)); break;
                case 5 : userRatingIV.setImageDrawable(getResources().getDrawable(R.drawable.stars5)); break;
            }

            TextView commentTV = (TextView) opinionView.findViewById(R.id.commentComment);
            commentTV.setText(commentsSplitter.nextToken());
            opinionsParentLayout.addView(opinionView);
        }
    }       

    parentLayout.addView(view);
4

0 回答 0