如何在Gridview中从sqlite加载图像和文本

时间:2013-07-12 06:56:32

标签: android gridview custom-adapter

我正在开发一个Android项目,其中图像显示在Gridview中,这是硬编码的

我想从外部源加载图像,下面是Adapter类从可绘制资源中获取图像的代码。另外我需要显示每个图像的一些文本,我是android开发的新手,所以请建议一种加载方法从sqlite数据库(用于文本)和外部存储(用于图像)

在Gridview中动态显示文本和图像
    package com.android.tabapp;

    import android.content.Context;
    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 Context mContext;

    public Integer[] mThumbIds = {
        R.drawable.pic_1, R.drawable.pic_2,
        R.drawable.pic_3, R.drawable.pic_4,
        R.drawable.pic_5, R.drawable.pic_6,
        R.drawable.pic_7, R.drawable.pic_8,
        R.drawable.pic_9, R.drawable.pic_10,
        R.drawable.pic_11, R.drawable.pic_12,
        R.drawable.pic_13, R.drawable.pic_14,
        R.drawable.pic_15
    };


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

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

   @Override
   public Object getItem(int position) {
    return mThumbIds[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(mThumbIds[position]);
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
       imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
       return imageView;
   }
 }

`

0 个答案:

没有答案