Note that there are some explanatory texts on larger screens.

plurals
  1. POGridView loading images again when onclick again
    primarykey
    data
    text
    <p>My problem is simple: when I click on the icon to show the GridView, it shows me grid of photos, then I back again and again I click on icon to see GridView and it's showing 2 grids with the same photos. For example if at first I had photo 1, photo 2 I press again and it shows photo 1, photo 2, photo 1, photo 2. I thought I could solve it by adding SharedPreferences (to check if user is in GridView for the first time). Is there any better, easier way to solve my problem? GridView's code:</p> <pre><code>public static List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); </code></pre> <p>...</p> <pre><code>items.add(new Item("Name1", "Text1", R.drawable.pic_12)); items.add(new Item("Name2", "Text2", R.drawable.pic_13)); items.add(new Item("Name3", "Text3", R.drawable.pic_14)); items.add(new Item("Name4", "Text4", R.drawable.pic_15)); items.add(new Item("Name5", "Text5", R.drawable.pic_2)); GridView gridView = (GridView) findViewById(R.id.gridview); gridView.setAdapter(new ImageAdapter(this, items)); </code></pre> <p>ImageAdapter:</p> <pre><code>public class ImageAdapter extends BaseAdapter { private List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); private LayoutInflater inflater; public ImageAdapter(Context context, List&lt;Item&gt; items) { inflater = LayoutInflater.from(context); this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int i) { return items.get(i); } @Override public long getItemId(int i) { return items.get(i).drawable; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View v = view; ImageView picture; TextView name; if (v == null) { v = inflater.inflate(R.layout.squareimageview, viewGroup, false); v.setTag(R.id.picture, v.findViewById(R.id.picture)); v.setTag(R.id.text, v.findViewById(R.id.text)); } picture = (ImageView) v.getTag(R.id.picture); name = (TextView) v.getTag(R.id.text); Item item = (Item) items.get(i); picture.setImageResource(item.getDrawable()); name.setText(item.name); return v; } } </code></pre> <p>Item class:</p> <pre><code>public class Item { String name; int drawable; String text; public int getDrawable() { return drawable; } public void setDrawable(int drawable) { this.drawable = drawable; } public Item(String name, String text, int id) { this.name = name; this.text = text; this.drawable = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre>
    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