Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:在先前添加的childview下添加TableLayout的子级(新行)_Java_Android_Grid Layout_Tablelayout_Custom Lists - Fatal编程技术网

Java Android:在先前添加的childview下添加TableLayout的子级(新行)

Java Android:在先前添加的childview下添加TableLayout的子级(新行),java,android,grid-layout,tablelayout,custom-lists,Java,Android,Grid Layout,Tablelayout,Custom Lists,我希望我的问题不要太含糊 我有一个问题,我已经被困了两天多了。我已经搜索了很多,尽管努力,我仍然没有找到解决方案 我的问题如下: 我有一个TableLayout,并使用ArrayList中的项目总数动态填充数据。瞬间,我的屏幕如下所示: 但我想要的是: 我尝试了很多不同的选择,例如: 自定义列表适配器 表格布局 网格布局 许多setLayoutParams选项 由于我的努力和代码,我想坚持使用TableLayout。GridLayout或任何其他..布局也不是问题 public void

我希望我的问题不要太含糊

我有一个问题,我已经被困了两天多了。我已经搜索了很多,尽管努力,我仍然没有找到解决方案

我的问题如下:

我有一个TableLayout,并使用ArrayList中的项目总数动态填充数据。瞬间,我的屏幕如下所示:

但我想要的是:

我尝试了很多不同的选择,例如:

  • 自定义列表适配器
  • 表格布局
  • 网格布局
  • 许多setLayoutParams选项
由于我的努力和代码,我想坚持使用TableLayout。GridLayout或任何其他..布局也不是问题

public void getTableLayoutDinnerProducts() {
    tableLayout = (TableLayout)findViewById(R.id.tableLayoutDinner);
    //gridLayout = (GridLayout)findViewById(R.id.gridLayoutDinner);
    //gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
    //gridLayout.setColumnCount(2);
    //gridLayout.setRowCount(3);
    //TableLayout.LayoutParams newTableLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
    //newTableLayout.setMargins(400, 0, 0, 0);

    // Creating a table row for each product in productController.
    for (final Product product : productController.getStaticProducts()) {
        if(product.getCategoryType() == CategoryType.Dinner){

            // Create tablerows
            tableRow = new TableRow(this);
            tableRow.setId(product.getID() + 1);
            tableRow.setBackgroundColor(Color.WHITE);
            tableRow.setLayoutParams(new Toolbar.LayoutParams(300, 300));

            // Create content for tablerows
            textArray = new TextView(this);
            textCaloriesArray = new TextView(this);
            textProductArray = new TextView(this);

            // Add image for each row
            image = new ImageView(this);

            bitmap = BitmapFactory.decodeResource(getResources(), product.getImage());
            int width=400;
            int height=400;
            resizedbitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
            image.setImageBitmap(resizedbitmap);
            image.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
            //image.setBackgroundResource(R.drawable.imagestyle);
            image.setAdjustViewBounds(true);
            tableRow.addView(image, 0, new TableRow.LayoutParams(400, 400));

            textArray.setText(product.getName());
            textCaloriesArray.setText(" Cal: " + String.valueOf(product.getCalories()));
            textProductArray.setText(" Prod: " + String.valueOf(product.getProductType()));
            textArray.setTextSize(18);
            textArray.setTypeface(null, Typeface.BOLD);
            textArray.setTextColor(Color.BLACK);
            textArray.setPadding(0, 150, 0, 100);

            //TableRow.LayoutParams params = (TableRow.LayoutParams)textArray.getLayoutParams();
            //params.span = 4;
            //textArray.setLayoutParams(params); // causes layout update

            tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
            tableRow.addView(textArray, 1, new TableRow.LayoutParams(200, 500));
            tableRow.addView(textCaloriesArray, 2, new TableRow.LayoutParams(200, 400));
            tableRow.addView(textProductArray, 3, new TableRow.LayoutParams(200, 400));

            tableRow.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    final View background = v;
                    background.setBackgroundColor(Color.RED);

                    final Dialog dialog = new Dialog(DinnerScreen.this);
                    dialog.setContentView(R.layout.popup);
                    dialog.show();

                    dismisspopup = (Button)dialog.findViewById(R.id.dismissPopup);
                    dismisspopup.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                            background.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
                        }
                    });

                    removeproduct = (Button)dialog.findViewById(R.id.removeProduct);
                    removeproduct.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(v.getContext(), DinnerScreen.class);
                            dialog.dismiss();
                            products = productController.getStaticProducts();
                            products.remove(product);
                            productController.setStaticProducts(products);
                            productController.getStaticProducts();

                            intent.putExtra("totalProducts", products);
                            intent.putExtra("totalCalories", String.valueOf(calorieCounter.getCountcalories()));
                            overridePendingTransition( 0, 0);
                            startActivity(intent);
                            overridePendingTransition( 0, 0);
                            showToastMessage("Product has been removed");
                        }
                    });

                    return false;
                }
            });

            tableRow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    calorieCounter.addCalories(product.getCalories());
                    changeStatusBar(calorieCounter.getCountcalories());
                }
            });
            //gridLayout.addView(tableRow);
            tableLayout.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        }
    }
}
因为我是动态添加的,所以没有.XML文件来添加宽度或位置

这个问题一定有解决办法。只是我再也找不到了


提前感谢

是否有任何特定要求您添加不使用xml的运行时视图,对于上面的视图,您可以简单地使用recyclerview,否则使用您的方法将是混乱和复杂的,因为您必须首先装箱一个布局,然后将所有textview添加到布局,并将最终布局添加到tablerow,或者只需将膨胀的xml视图添加到tablerow,而我对此没有具体要求。当我在这几天取得一些进展时,我注意到这不是最好的方法。基本上我已经完成了我的应用程序,但布局是目前唯一困扰我的问题。所以我想也许有一个简单的解决方案来修复布局,我正在监督。但您建议使用recyclerview?是的,如果不建议使用TableLayout,那么recyclerview可以更快、更高效地处理大型数据。您最好切换到recyclerview,但如果您想使用必须在xml上创建的相同方法,请在表行中添加该运行时,这取决于您自己。