Android Gallery显示所选图像

Android Gallery显示所选图像,android,android-layout,Android,Android Layout,我输入了以下代码,我试图显示在库中选择的图像。但我得到的是默认的android图像,而不是我保存的图像。我做错了什么?如何解决此问题 package HGallery.com; import HGallery.com.R; import HGallery.com.HGalleryActivity.ImageAdapter; import android.app.Activity; import android.os.Bundle; import android.content.Context

我输入了以下代码,我试图显示在库中选择的图像。但我得到的是默认的android图像,而不是我保存的图像。我做错了什么?如何解决此问题

package HGallery.com;

import HGallery.com.R;
import HGallery.com.HGalleryActivity.ImageAdapter;
import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class HGalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ImageView iv=(ImageView) findViewById(R.id.imageView1);
    iv.setVisibility(View.INVISIBLE);



    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));

    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            //Toast.makeText(HGalleryActivity.this, "here", Toast.LENGTH_SHORT).show();
            //Toast.makeText(HGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            iv.setVisibility(View.VISIBLE);
            Drawable d=getResources().getDrawable(R.drawable.icon);
            //Toast.makeText(HGalleryActivity.this, ""+d, Toast.LENGTH_SHORT).show();
            iv.setImageDrawable(d);     


        }
    });



}
public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3

    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }

/*  public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        return null;
    }*/
}
}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
  <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView  android:id="@+id/imageView1" android:layout_width="320px" android:layout_height="250px"></ImageView>
<!-- <ImageView android:layout_width="wrap_content" android:id="@+id/imageView2" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>-->

应修改ImageAdapter中名为public Object getItemint position的方法,以使用position参数返回适当的mimageId[]。然后,在onItemClick内部,可以调用parent.getItemAtPositionposition来获取资源id


如果您想将该方法用于ID以外的其他内容,可以编写自己的get方法,我将其称为getResourceId,并调用ImageAdapterparent.getAdapter.getResourceIdposition。

不确定这是否相关,但您的onItemClick方法没有引用所单击的特定项目。它仅指可绘制文件夹中的某个图像,不管图像名为icon是什么。icon是默认情况下可用的图像。让我在LCIK上这样问一下,我如何用当前选择的图像填充图像视图?嗨,你能给我看一下你所说的第一个实现的代码吗?非常简单。只需更换返回位置;具有返回mimageid[位置];并更改返回类型,因为它不是方法签名的一部分,尽管您可以将其保留为对象,只强制转换结果。在onItemClick中,将行更改为Drawable d=getResources.getDrawableparent.getItemAtPositionposition;