Reputation: 21
My app needs to list texts and images for one particular activity. Each row of the list has 2 columns - 1 for image and 1 for string.
It works if the number of images is small (ten or less). When it exceeds 10 (sometime 15), the app will crash. How can make this work for more than ten images?
Below are the codes for listing the texts and images. The first 2 lines retrieve the data from strings.xml. Thanks.
pic=res.obtainTypedArray(R.array.site_pic);
arr_site = getResources().getStringArray(R.array.site_name);
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
for(int i=0; i<num_site; i++)
{
final TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.setPadding(0, 10, 0, 10);
ImageView iv = new ImageView(this);
iv.setImageDrawable(pic.getDrawable(i));
iv.setAdjustViewBounds(true);
iv.setMaxHeight(80);
iv.setPadding(5, 5, 15, 5);
tr.addView(iv);
TextView tv1 = new TextView(this);
tv1.setText(arr_site[i]);
tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv1.setGravity(Gravity.LEFT);
tr.addView(tv1);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
Upvotes: 2
Views: 1174
Reputation: 162
Use ViewHolder
logic. When you use ViewHolder, your images just replace with the others while scrolling.
Also, I suggest you that you can use GridView
to show items like you said.
Here is the reference: http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Here is a tutorial: http://www.learn2crack.com/2014/01/android-custom-gridview.html
Upvotes: 1
Reputation: 31
If your image sizes are high resolution, that is possibly the issue. From my experience, doing anything possible to reduce the image size - but maintain quality is essential.
Also, if all else fails an easy fix (if you haven't tried it) is to add
android:largeHeap="true"
within the <application>
tag of your AndroidManifest.xml file.
Here is an example from my own manifest file:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:largeHeap="true">
Upvotes: 3