我GridView里面有一个ImageView和两个TextView。ImageView里的内容我是读取SDCard卡里的图片。但是我不知道怎么在GridView里怎么绑定这个ImageView。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">
    <GridView 
     android:id="@+id/GridView01" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:verticalSpacing="5dip"
     android:horizontalSpacing="5dip"
     android:stretchMode="columnWidth"/>
</LinearLayout>
grid_row.xml<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/imageView01" android:layout_width="30.0dip"
  android:layout_height="30.0dip" android:scaleType="fitCenter"
  android:layout_alignParentLeft="true" />
<TextView android:id="@+id/txtLink1"
  android:paddingLeft="6.0dip" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_toRightOf="@id/imageView01" />
<TextView android:id="@+id/txtLink2"
  android:paddingLeft="6.0dip" android:paddingBottom="8.0dip"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_marginTop="-4.0dip" android:layout_toRightOf="@id/imageView01"
  android:layout_below="@id/txtLink1" />
</RelativeLayout>
Main.javapackage com.myAndroid.execrise;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
public class Main extends Activity 
{
  
    private List<? extends Map<String, ?>> generateDataList()
    {
     ArrayList<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
     
     for(int i=0;i<2;i++)
     {
      HashMap<String,Object> hmap=new HashMap<String,Object>();
      String imagepath="/sdcard/abc.jpg";
      Bitmap bit = BitmapFactory.decodeFile(imagepath);
      hmap.put("col1", bit);
      hmap.put("col2", "QQ"+(i+2));
      hmap.put("col3", "QQ"+(i+3));
      list.add(hmap);
     }
     return list;
}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  GridView gv=(GridView)this.findViewById(R.id.GridView01);   
  SimpleAdapter sca=new SimpleAdapter(
             this,
             generateDataList(), //数据List
             R.layout.grid_row, //行对应layout id
             new String[]{"col1","col2","col3"}, //列名列表
             new int[]{R.id.imageView01,R.id.txtLink1,R.id.txtLink2}//列对应控件id列表
        );
  gv.setAdapter(sca);//为GridView设置数据适配器
    }
}应该怎么处理请大家帮帮忙!

解决方案 »

  1.   

    继承BaseAdapter,看看apidemos中有
      

  2.   

    能帮忙简单写个示例吗!我是刚学Andorid不久!!确实知道甚少!
      

  3.   

    晕~~~ CSDN这个Android论坛还真清净!还真没人会!
      

  4.   

    看下API中的Resources的Hello Views中的GridView实例吧
      

  5.   

    这里边image我用的固定的,你可以在实例化adapter的时候传进来替换掉
    public class AppsAdapter extends BaseAdapter {
    private Integer[] mImages = { R.drawable.icon_1, R.drawable.icon_2,
    R.drawable.icon_3, R.drawable.icon_4, R.drawable.icon_5,
    R.drawable.icon_6, R.drawable.icon_7, R.drawable.icon_8 }; // 要显示的图片
    private Integer[] titles = { R.string.findall, R.string.enhance_system,
    R.string.answer, R.string.tools, R.string.help,
    R.string.message_center, R.string.subsidiary_function, R.string.book_tools };// 图片对应的名称
    private Context context; public AppsAdapter(Context context) {
    this.context = context;
    } public int getCount() {
    // TODO Auto-generated method stub
    return mImages.length;
    } public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    } public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    } public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    TextView tv = null;
    if ((tv = (TextView) convertView) == null) {
    tv = new TextView(context);
    }
    tv.setTextColor(Color.WHITE);
    // tv.setHeight(150);
    tv.setTextSize(20);
    Bitmap app_image = BitmapFactory.decodeResource(getResources(),
    mImages[position]);
    tv.setCompoundDrawablesWithIntrinsicBounds(null,
    new BitmapDrawable(app_image), null, null);// 设置图片在上边
    tv.setText(titles[position]);
    // tv.setGravity(Gravity.CENTER);// 设置文字居中
    tv.setGravity(Gravity.CENTER_HORIZONTAL);
    return tv;
    }
    }
      

  6.   

    楼主的问题是她的文本可以显示出来,图片显示不了。
    问题在于
    Bitmap bit = BitmapFactory.decodeFile(imagepath);
          hmap.put("col1", bit);
    bit是一个Bitmap类型,如果是id的话就能正常显示了。
    现在就需要把Bitmap怎么转换为一个id值。
      

  7.   

    就好像ListView一样,可以做一个每个条目显示的布局文件。这样你就可以在这个布局文件中添加你的ImageView,TextView……
      

  8.   

    修改了adapter类,实现了楼主的要求。
    修改:
    1.grid_row.xml增加一个id
    <RelativeLayout
     android:id="@+id/rlGridRow"2.java文件重写了GridView的适配器类ImageAdapter
    public class AndroidTest2_3_3 extends Activity {
    private static final String TAG = "AndroidTest2_3_3";
    private Bitmap bit;
    private String textArray1[] = {"QQ1", "QQ2"};
    private String textArray2[] = {"QQ3", "QQ4"};

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            String imagepath = Environment.getExternalStorageDirectory() + "/abc.jpg";
        bit = BitmapFactory.decodeFile(imagepath);
          
            GridView gv=(GridView)this.findViewById(R.id.GridView01);   
            Log.d(TAG, "+++++++++++1");
            ImageAdapter ia = new ImageAdapter(this);
            Log.d(TAG, "+++++++++++2");
            gv.setAdapter(ia);//为GridView设置数据适配器
            Log.d(TAG, "+++++++++++3");
            
        }
        
        public class ImageAdapter extends BaseAdapter {
        private Context mContext;
        public ImageAdapter(Context context) {
        Log.d(TAG, "ImageAdapter 1");
            mContext = context;
            Log.d(TAG, "ImageAdapter 2");
        }
       
        public int getCount() { 
            return textArray1.length;
        }
       
        public Object getItem(int position) {
            return position;
        }
       
        public long getItemId(int position) {
            return position;
        }
       
        public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(TAG, "000000000000000");
        View view = View.inflate(AndroidTest2_3_3.this, R.layout.grid_row, null);
        Log.d(TAG, "1111111111111");
        RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.rlGridRow);
        Log.d(TAG, "2222222222222");
        ImageView image = (ImageView)rl.findViewById(R.id.imageView01);
        image.setImageBitmap(bit);
        TextView tv1 = (TextView)rl.findViewById(R.id.txtLink1);
        tv1.setText(textArray1[position]);
        TextView tv2 = (TextView)rl.findViewById(R.id.txtLink2);
        tv2.setText(textArray2[position]);
        return rl;
        }

    }
      

  9.   

    首先获取SD卡图片:private List<String> getImagesFromSD() {
    List<String> imageList = new ArrayList<String>(); File f = new File("/sdcard/");
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
    f = new File(Environment.getExternalStorageDirectory().toString());
    }
    else
    {
    Toast.makeText(ACDsee.this, R.string.sdcarderror, 1).show();
    return imageList;
    } File[] files = f.listFiles(); if(files == null || files.length == 0)
    return imageList;
    /**
     * 将所有图像文件的路径存入ArrayList列表
     */
    for (int i = 0; i < files.length; i++) {
    File file = files[i];
    if (isImageFile(file.getPath()))
    imageList.add(file.getPath());
    }
    return imageList;
    }
    然后传递给ImageList = getImagesFromSD();
    grid.setAdapter(new ImageAdapter(this, ImageList);在public View getView(int position, View convertView, ViewGroup parent)中
      
    Bitmap Image = BitmapFactory.decodeFile(ImageList.get(position));
    ImageView imageView = new ImageView(mContext);
    imageView.setImageBitmap(Image);