sam2017 2014-11-14 03:39 采纳率: 0%
浏览 1779

新手--gallery怎么不能显示图片

package com.example.gallery;

import android.app.Activity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
private Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery=(Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));//设置图片适配器
//点击图片监听
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            Toast.makeText(MainActivity.this, "点击了第"+arg2+"张图片", 
                    Toast.LENGTH_LONG).show();
        }

});

}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {


}

}

//Adpater的class

package com.example.gallery;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{
private Context context;
private Integer[] ima;

public ImageAdapter(Context context){
    this.context=context;
    ima=new Integer[]{

                    R.drawable.ima01,
                    R.drawable.ima02,
                    R.drawable.ima03


    };
}

@Override
public int getCount() {
    // 返回获得的图片数目
    return ima.length;
}

@Override
public Object getItem(int position) {

    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageview=new ImageView(context);
    imageview.setImageResource(position);
    imageview.setScaleType(ImageView.ScaleType.FIT_XY);
    imageview.setLayoutParams(new Gallery.LayoutParams(136,88));

    return imageview;
}

}

//xml

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   android:gravity="center_vertical"
   android:background="?android:galleryItemBackground"

/>

求各位师兄师姐解答,谢谢啦

  • 写回答

1条回答 默认 最新

  • _LittleMing 2014-11-22 12:14
    关注

    public Object getItem(int position) {

    return position;
    

    }
    这里返回的应该是ima[position]

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog