Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:38:10+00:00 2026-06-04T07:38:10+00:00

Currently I am looking at http://developer.android.com/resources/tutorials/views/hello-gridview.html I created test project with additional logs in

  • 0

Currently I am looking at http://developer.android.com/resources/tutorials/views/hello-gridview.html
I created test project with additional logs in onCreate(), onResume and getView():

HelloGridViewActivity.java

package com.hello.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class HelloGridViewActivity extends Activity {
    private static final String TAG = "HelloGridViewActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(HelloGridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });

        Log.e(TAG, "onCreate()");
    }

    protected void onResume() {
        super.onResume();

        Log.e(TAG, "onResume()");
    }
}

ImageAdapter.java

package com.hello.namespace;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private static final String TAG = "ImageAdapter";
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            Log.e(TAG, "position = " + position);
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };

}

The log output:

05-23 08:59:13.541: E/HelloGridViewActivity(733): onCreate()
05-23 08:59:13.551: E/HelloGridViewActivity(733): onResume()
05-23 08:59:13.711: E/ImageAdapter(733): position = 0
05-23 08:59:14.380: E/ImageAdapter(733): position = 1
05-23 08:59:15.190: E/ImageAdapter(733): position = 2
05-23 08:59:15.771: E/ImageAdapter(733): position = 3
05-23 08:59:16.340: E/ImageAdapter(733): position = 4
05-23 08:59:17.141: E/ImageAdapter(733): position = 5
05-23 08:59:17.721: E/ImageAdapter(733): position = 6
05-23 08:59:18.330: E/ImageAdapter(733): position = 7
05-23 08:59:18.611: E/ImageAdapter(733): position = 8
05-23 08:59:18.631: E/ImageAdapter(733): position = 9
05-23 08:59:18.631: E/ImageAdapter(733): position = 10
05-23 08:59:18.651: E/ImageAdapter(733): position = 11
05-23 08:59:18.651: E/ImageAdapter(733): position = 12
05-23 08:59:18.671: E/ImageAdapter(733): position = 13
05-23 08:59:18.681: E/ImageAdapter(733): position = 14
05-23 08:59:18.691: E/ImageAdapter(733): position = 15
05-23 08:59:18.701: E/ImageAdapter(733): position = 16
05-23 08:59:18.721: E/ImageAdapter(733): position = 17
05-23 08:59:18.721: E/ImageAdapter(733): position = 18
05-23 08:59:18.741: E/ImageAdapter(733): position = 19
05-23 08:59:18.741: E/ImageAdapter(733): position = 20
05-23 08:59:18.891: E/ImageAdapter(733): position = 0

And here are two questions:

  1. Why ImageView at position 0 was created twice?

  2. Let’s say I want to do something with ImageViews. I’d create ImageView array, initialize its elements in getView(), but I would not be able to use it in onCreate() or even in onResume(), because ImageViews don’t exist yet! What should I do?

Sorry for my English.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:38:11+00:00Added an answer on June 4, 2026 at 7:38 am

    1: I assume that you scroll around your List to get this Log (because getView is called only if the View is visible), so its likely that you just scrolled up again, and View at pos 0 was scrolled on top out and is coming from top again…

    2: Depends on WHY you want to have your Views in onCreate or onResume. If there is no other solution and you MUST have them, you could simply create all Views yourself in onCreate save them inside a List and just return it in getView. I would try to avoid this, because the ListView instatiates Views ONLY if there are Visible and reuses them. You’ll loose this feature in the implementation I described, so please tell us, WHY you wanna have it in before getView

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently looking at the EventQueue class on the Oracle website: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/EventQueue.html But I'm
I'm currently working with CKEditor ( http://ckeditor.com/ ). I'm looking for: 1) an exhaustive
I'm currently looking at the miranda upnp tool (http://code.google.com/p/mirandaupnptool/). Is there a way for
http://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html I'm new to IOS development, and have been playing around with the IOS
I'm currently looking at a hefty Rails test suite. It's nothing I can get
I am currently looking into spliting a very long string that could contain HTML
I'm currently using the ARKit ported by the http://iphonear.org guys, looping through a fetched
Currently looking into learn new technology and silverlight is on the potential list. However,
I'm currently looking at different solutions getting 2 dimensional mathematical formulas into webpages. I
I'm currently looking at ways to allow people to select multiple files at once

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.