Note that there are some explanatory texts on larger screens.

plurals
  1. POLoader doesn't stop adding views to list items when scrolling up and down
    primarykey
    data
    text
    <p>I try to load ListView items with a custom CursorLoader into a list. The problem is, if I scroll to the bottom and up again Android adds multiple <strong>new images</strong> to every list item. My question is, how to prevent Android to load more images into the list.<br> Column 1 should always have only one image, column 2 two images...</p> <p><strong>Main.java</strong> </p> <pre><code>public class Main extends FragmentActivity implements LoaderManager.LoaderCallbacks&lt;Cursor&gt; { private Context mCtx = null; private MyAdapter mAdapter = null; private final static String COLUMN_A = "a"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mCtx = this; mAdapter = new MyAdapter(mCtx); ((ListView) findViewById(R.id.listview)).setAdapter(mAdapter); getSupportLoaderManager().initLoader(0, null, this); } @Override public Loader&lt;Cursor&gt; onCreateLoader(int loader_id, Bundle bundle) { return new MyCursorLoader(mCtx) { @Override public Cursor loadInBackground() { MatrixCursor m = new MatrixCursor(new String[]{"_id", COLUMN_A}); m.addRow(new String[]{"1", "Column 1"}); m.addRow(new String[]{"2", "Column 2"}); m.addRow(new String[]{"3", "Column 3"}); m.addRow(new String[]{"4", "Column 4"}); m.addRow(new String[]{"5", "Column 5"}); return m; } }; } @Override public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor c) { mAdapter.changeCursor(c); } @Override public void onLoaderReset(Loader&lt;Cursor&gt; loader) { mAdapter.changeCursor(null); } private class MyAdapter extends CursorAdapter { private final LayoutInflater mInflater; public MyAdapter(final Context ctx) { super(ctx, null, 0); mInflater = LayoutInflater.from(ctx); } @Override public View newView(Context ctx, Cursor c, ViewGroup parent) { return mInflater.inflate(R.layout.item, parent, false); } @Override public void bindView(final View v, final Context ctx, final Cursor c) { LinearLayout parent = (LinearLayout) v; final int position = c.getPosition(); Log.i("MyAdapter", "Cursor position: "+ position + "\tImages: "+ (parent.getChildCount() -1)); ((TextView) v.findViewById(R.id.content)) .setText(c.getString(c.getColumnIndex(COLUMN_A))); LinearLayout ll = new LinearLayout(ctx); ll.setOrientation(LinearLayout.VERTICAL); for (int i=0; i&lt;(position+1); i++) { SystemClock.sleep(i * 100); ImageView img = new ImageView(ctx); img.setImageResource(R.drawable.ic_launcher); ll.addView(img); } parent.addView(ll); } } private abstract class MyCursorLoader extends CursorLoader { public MyCursorLoader(Context ctx) { super(ctx); } @Override public abstract Cursor loadInBackground(); } </code></pre> <p>}</p> <p><strong>activity_main.xml</strong> </p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Main" &gt; &lt;ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>item.xml</strong> </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="30dip" android:text="---" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><br> <strong>↓ after trying Diogo Bento suggestion on a Galaxy S1:</strong> <img src="https://i.stack.imgur.com/RGy3m.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload