Android 从JSON响应创建动态布局

Android 从JSON响应创建动态布局,android,layout,dynamic,Android,Layout,Dynamic,这件事我已经忙了好几天了。我已经做了大量的研究,尝试了很多不同的方法,但我似乎无法让它发挥作用 我试图做的是在处理JSON响应中的多行时在for循环中设置一个动态布局 返回五个数据库字段,包括web服务器上图像的路径 问题不是分配和显示数据和图像,而是将其全部显示在正确的位置 我的代码被还原为一个可怕的基本表布局和多行 下面是它现在正在做的事情……(很抱歉不能发布图像,但显然我需要一些堆栈点来实现这一点-每行代表一个表行) |图像| |数据| |数据| |数据数据| 这里是代码(我知道这不是我需

这件事我已经忙了好几天了。我已经做了大量的研究,尝试了很多不同的方法,但我似乎无法让它发挥作用

我试图做的是在处理JSON响应中的多行时在for循环中设置一个动态布局

返回五个数据库字段,包括web服务器上图像的路径

问题不是分配和显示数据和图像,而是将其全部显示在正确的位置

我的代码被还原为一个可怕的基本表布局和多行

下面是它现在正在做的事情……(很抱歉不能发布图像,但显然我需要一些堆栈点来实现这一点-每行代表一个表行)

|图像|
|数据|
|数据|
|数据数据|

这里是代码(我知道这不是我需要的)来设置这个

                   for (int i = 0; i < array.length(); i++) {
                try {
                   //all my json work is here
                   TableLayout tl = (TableLayout)findViewById(R.id.bottlelisttablelayout);
                          //create image table row for now
                  TableRow imagetr = new TableRow(this);
                  imagetr.setLayoutParams(new LayoutParams(
                                  LayoutParams.WRAP_CONTENT,
                                  LayoutParams.WRAP_CONTENT));
                   final ImageView bottleiv = new ImageView(this);
                   bottleiv.setId(bottleID);
                   bottleiv.setLayoutParams(new LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                   bottleiv.getLayoutParams().height=75;
                   bottleiv.getLayoutParams().width=75;
                   bottleiv.setAdjustViewBounds(true);
                   bottleiv.setScaleType(ScaleType.CENTER_INSIDE);
                   String imageURL = bottlePicture;
                   Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
                   bottleiv.setImageBitmap(bitmap);
                   bottleiv.setVisibility(View.VISIBLE);
                   imagetr.addView(bottleiv);
                   imagetr.setClickable(true);

                   /* Create a new row to be added. */
                   TableRow tr = new TableRow(this);
                   tr.setLayoutParams(new LayoutParams(
                                  LayoutParams.FILL_PARENT,
                                  LayoutParams.WRAP_CONTENT));

                   final TextView bottleTextView = new TextView(this);
                   bottleTextView.setId(bottleID);
                   bottleTextView.setText(Html.fromHtml(data_field1));
                   bottleTextView.setLayoutParams(new LayoutParams(
                           LayoutParams.FILL_PARENT,
                           LayoutParams.WRAP_CONTENT,
                           1.0f));
                   bottleTextView.setTextColor(Color.BLACK);
                   bottleTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                   bottleTextView.setTextSize(12);
                   tr.setPadding(0, 0, 0, 0);  //left, top, right, bottom
                   tr.addView(bottleTextView);
                   tr.setClickable(true);

                   TableRow tr2 = new TableRow(this);
                   tr2.setLayoutParams(new LayoutParams(
                                  LayoutParams.FILL_PARENT,
                                  LayoutParams.WRAP_CONTENT));
                   final TextView bottleTextView2 = new TextView(this);
                   bottleTextView2.setId(bottleID);
                   bottleTextView2.setText(Html.fromHtml("some text"+datafield2));
                   bottleTextView2.setLayoutParams(new LayoutParams(
                           LayoutParams.FILL_PARENT,
                           LayoutParams.WRAP_CONTENT,
                           1.0f));
                   bottleTextView2.setTextColor(Color.GRAY);
                   bottleTextView2.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                   bottleTextView2.setTextSize(8);
                   tr2.setPadding(0, 0, 0, 0);  //left, top, right, bottom
                   tr2.addView(bottleTextView2);
                   tr2.setClickable(true);

                   TableRow tr3 = new TableRow(this);
                   tr2.setLayoutParams(new LayoutParams(
                                  LayoutParams.FILL_PARENT,
                                  LayoutParams.WRAP_CONTENT));
                   final TextView bottleTextView3 = new TextView(this);
                   bottleTextView3.setId(bottleID);
                   bottleTextView3.setText(Html.fromHtml("some text: "+datafield3));
                   bottleTextView3.setLayoutParams(new LayoutParams(
                           LayoutParams.FILL_PARENT,
                           LayoutParams.WRAP_CONTENT,
                           1.0f));
                   bottleTextView3.setTextColor(Color.GRAY);
                   bottleTextView3.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                   bottleTextView3.setTextSize(8);
                   tr3.setPadding(0, 0, 0, 0);  //left, top, right, bottom
                   tr3.addView(bottleTextView3);
                   tr3.setClickable(true);

                   TableRow tr4 = new TableRow(this);
                   tr2.setLayoutParams(new LayoutParams(
                                  LayoutParams.FILL_PARENT,
                                  LayoutParams.WRAP_CONTENT));
                   final TextView bottleTextView4 = new TextView(this);
                   bottleTextView4.setId(bottleID);
                   bottleTextView4.setText(Html.fromHtml("some text"+datafield4));
                   bottleTextView4.setLayoutParams(new LayoutParams(
                           LayoutParams.FILL_PARENT,
                           LayoutParams.WRAP_CONTENT,
                           1.0f));
                   bottleTextView4.setTextColor(Color.BLACK);
                   bottleTextView4.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                   bottleTextView4.setTextSize(12);
                   final TextView bottleTextView5 = new TextView(this);
                   bottleTextView5.setId(bottleID);
                   bottleTextView5.setText(Html.fromHtml("some text"+datafield5));
                   bottleTextView5.setLayoutParams(new LayoutParams(
                           LayoutParams.FILL_PARENT,
                           LayoutParams.WRAP_CONTENT,
                           1.0f));
                   bottleTextView5.setTextColor(Color.BLACK);
                   bottleTextView5.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                   bottleTextView5.setTextSize(12);
                   tr4.setPadding(0, 0, 0, 0);  //left, top, right, bottom
                   tr4.addView(bottleTextView4);
                   tr4.addView(bottleTextView5);
                   tr4.setClickable(true);

                   imagetr.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            Intent intent = new Intent(getApplicationContext(), SingleBottleDisplay.class);
                            //Toast.makeText(GuruBottles.this,"bottleID = "+headerTextView.getId(), Toast.LENGTH_SHORT).show();
                            intent.putExtra("id", bottleTextView.getId());
                            startActivityForResult(intent,0);
                        }
                    });
                   tr.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            Intent intent = new Intent(getApplicationContext(), SingleBottleDisplay.class);
                            //Toast.makeText(GuruBottles.this,"bottleID = "+headerTextView.getId(), Toast.LENGTH_SHORT).show();
                            intent.putExtra("id", bottleTextView.getId());
                            startActivityForResult(intent,0);
                        }
                    });
                       tr2.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                Intent intent = new Intent(getApplicationContext(), SingleBottleDisplay.class);
                                //Toast.makeText(GuruBottles.this,"bottleID = "+headerTextView.getId(), Toast.LENGTH_SHORT).show();
                                intent.putExtra("id", bottleTextView.getId());
                                startActivityForResult(intent,0);
                            }
                        });
                       tr3.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                Intent intent = new Intent(getApplicationContext(), SingleBottleDisplay.class);
                                //Toast.makeText(GuruBottles.this,"bottleID = "+headerTextView.getId(), Toast.LENGTH_SHORT).show();
                                intent.putExtra("id", bottleTextView.getId());
                                startActivityForResult(intent,0);
                            }
                        });
                       tr4.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                Intent intent = new Intent(getApplicationContext(), SingleBottleDisplay.class);
                                //Toast.makeText(GuruBottles.this,"bottleID = "+headerTextView.getId(), Toast.LENGTH_SHORT).show();
                                intent.putExtra("id", bottleTextView.getId());
                                startActivityForResult(intent,0);
                            }
                        });

                      /* Add row to TableLayout. */
                       tl.addView(imagetr,new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                       tl.addView(tr,new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                       tl.addView(tr2,new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                       tl.addView(tr3,new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                       tl.addView(tr4,new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
} catch (JSONException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
for(int i=0;i<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottlelistlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/background"
    android:layout_gravity="center">
    <TextView
        android:id="@+id/guruTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/guru"
        android:textStyle="bold"
        android:textSize="15dp"
        android:textColor="#FFFFFF"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="1.5"
        android:gravity="center" />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/guruTitle"
        android:layout_above="@id/radiogroup"
        android:scrollbarStyle="outsideInset"
        android:background="#ffffff" >
         <TableLayout
            android:id="@+id/bottlelisttablelayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="75dp"
            android:layout_gravity="right"
            android:layout_marginBottom="35dp"
            android:shrinkColumns="0" >
         </TableLayout>
     </ScrollView>
    <include layout="@layout/actnavbar" />
</RelativeLayout>