从JSON响应创建动态布局

时间:2012-01-01 21:17:09

标签: android layout dynamic

我几天来一直在敲打这个。我做了大量的研究并尝试了一大堆不同的方法,但我似乎无法让它发挥作用。

我要做的是在for循环中设置动态布局,同时处理JSON响应中的多行。

返回五个数据库字段,包括我的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();
        }

这是我的xml代码......

<?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>

这是我真正需要的......图像位于所有四个数据行的左侧。

| |数据
| |数据
|图片|数据
| |数据数据

有人可以指出我正确的方向吗?

非常感谢我提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

您可以在单独的xml文件中定义要显示的单个项目(这将帮助您设计视图),然后将xml动态加载到TableLayout或ListView(我更喜欢后者)。

您是否考虑使用适配器(如BaseAdapter)来执行此操作?