2012-01-01 12 views
2

私は数日間、私の頭を叩きました。私は数多くの研究を行い、さまざまなアプローチを試みましたが、これを機能させることはできません。JSONレスポンスからダイナミックレイアウトを作成する

私がやっているのは、JSONレスポンスで複数の行を処理しているときに、forループ内に動的レイアウトを設定することです。

Webサーバー上の画像へのパスを含め、5つのデータベースフィールドが返されます。

問題はデータと画像を割り当てて表示していないため、問題はすべて正しい位置に表示されています。

私のコードは、ひどく基本的なテーブルレイアウトと複数の行に戻されます。

これは今行っていることです...(画像を投稿できないことについて申し訳ありませんが、明らかに私はそれを行うためにいくつかのスタックポイントが必要です - 各行はテーブル行を表します)。

|      イメージ      |
|        データ        |
|        データ        |
|  データ データ|

、ここでこれは私のXMLコードであるコード(私は必要なものではありません知っている)これを設定するには...

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

ここ私が実際に必要とするものです。画像は、4つのデータ行すべての左にあります。

|                   |      データ
|                   |      データ
| image |      データ
|                   |データ   データ

誰かが正しい方向に私を指すしてくださいもらえますか?

私が得ることができるあらゆる事前のおかげで多くの感謝。

答えて

2

個別のxmlファイル(ビューのデザインに役立ちます)に表示する個々の項目を定義し、xmlをTableLayoutまたはListView(これは後者が好きです)に動的に読み込むことができます。

アダプター(BaseAdapterなど)を使用してこれを行うことを考えましたか?

+0

私はxml includeを昨日試しましたが、動作させることができませんでした。私はすべてを引き裂き、最初から始めなければならないかもしれません。 – Rmilligan2372

+0

あなたのアイデアは完璧に働いたLee - ありがとう! – Rmilligan2372

+0

私は助けることができてうれしい;) –

関連する問題