图像没有在android中的模拟器中显示

时间:2014-04-06 10:27:10

标签: android eclipse gridview

我是android新手。我坚持参加这个计划。请帮我!。在我的应用程序中,如果我正在显示一张照片,它将在模拟器中显示但不止一张照片显示“不幸停止”。我的代码是:

ImageAdapter.java

公共类ImageAdapter扩展了BaseAdapter {

私有Context mContext;

public Integer[] pics ={
                R.drawable.maysoonn, R.drawable.maysoonn1, 
        R.drawable.maysoonn2,
            R.drawable.maysoonn3, R.drawable.afass2, R.drawable.afass3, R.drawable.maysoonn4,
            R.drawable.afass, 
            R.drawable.afass1
        };

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

@Override

public int getCount(){
    return pics.length;  // return no. of images
}
@Override
public Object getItem(int position){
    return pics[position];
}
@Override
public long getItemId(int position){
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(pics[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70,70));
    return imageView;
}

}

GridViewPicture.java

公共类GridViewPicture扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_view_picture);

 GridView grid = (GridView)findViewById (R.id.gridView);

// create instance of ImageAdapter


 grid.setAdapter(new ImageAdapter(this));



}

}

布局xml

android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
 >

1 个答案:

答案 0 :(得分:0)

// try this way hope this will help you....
1. "GridView" Xml (activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:id="@+id/container">

    <GridView
        android:id="@+id/grdMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"/>

</LinearLayout>

2."GridView" Item XMl(grid_item.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imgGridItem"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"/>
</LinearLayout>

3.Activity (MainActivty.java)
public class MainActivity extends Activity{

    private GridView grdMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        grdMain = (GridView) findViewById(R.id.grdMain);
        grdMain.setAdapter(new ImageAdapter(this));
        grdMain.setNumColumns(3);

    }

    class ImageAdapter extends BaseAdapter{
        private Context mContext;
        public Integer[] pics ={
                R.drawable.maysoonn, R.drawable.maysoonn1,
                R.drawable.maysoonn2,
                R.drawable.maysoonn3, R.drawable.afass2, R.drawable.afass3, R.drawable.maysoonn4,
                R.drawable.afass,
                R.drawable.afass1
        };

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

        @Override

        public int getCount(){
            return pics.length;  // return no. of images
        }
        @Override
        public Object getItem(int position){
            return pics[position];
        }
        @Override
        public long getItemId(int position){
            return 0;
        }
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item,null,false);
                holder.imgGridItem = (ImageView) convertView.findViewById(R.id.imgGridItem);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
            holder.imgGridItem.setImageResource(pics[position]);

            return convertView;
        }
    }
    public static class ViewHolder
    {
        public ImageView imgGridItem;
    }
}