I've been banging my head on this one for a couple days. I've done a ton of research and tried a whole bunch of different approaches, but I just can't seem to get this to work.
What I'm trying to do is setup a dynamic layout in a for loop while processing multiple rows in a JSON response.
There are five database fields that get returned, including the path to an image on my web server.
The problem isn't assigning and displaying the data and image, the problem is displaying it all in the right position.
My code is reverted to a hideously basic table layout and multiple rows.
Here's what it's doing right now....(apologies for not being able to post an image, but evidently I need some stack points to do that - each line represents a table row).
| image |
| data |
| data |
| data data|
and here is the code (that I know is not what I need) to set this up...
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();
}
This is my xml code...
<?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>
Here is what I really need...where the image is to the left of all four data rows.
| | data
| | data
|image| data
| |data data
Could someone please point me in the right direction?
Many thanks in advance for any help I can get.