0

現在、私はhttp://developer.android.com/resources/tutorials/views/hello-gridview.htmlを見てい ますonCreate()、onResume、および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
    };

}

ログ出力:

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

そして、ここに2つの質問があります:

  1. 位置0のImageViewが2回作成されたのはなぜですか?

  2. ImageViews で何かをしたいとしましょう。ImageView 配列を作成し、その要素を getView() で初期化しますが、ImageView がまだ存在しないため、onCreate() または onResume() でさえ使用できません。私は何をすべきか?

私の英語でごめんなさい。

4

1 に答える 1