ImageAdapter的getView方法出错

时间:2012-03-23 06:04:42

标签: android sqlite gridview

我正在激活rawQuery以选择性别。如果性别是男性,那么男性照片应该部署在仪表板上。但是我在rawQuery语句中收到错误“NullPointerEcxeption”。 你能帮帮我吗?感谢。

ImageAdapter中的getView函数是

 public View getView(int position, View convertView, ViewGroup parent) 
{
    ImageView imageView = new ImageView(mContext);;
    HashMap<String, Integer> images = new HashMap<String, Integer>();    
    images.put( "male", Integer.valueOf( R.drawable.kidmale));
    images.put( "female", Integer.valueOf( R.drawable.kidfemale));    
    //String select="SELECT gender FROM db_mydata.tbl_mykid";
    Cursor res=db.rawQuery("SELECT gender FROM tbl_mykid " ,new String[]{gender}); 
    while(res.moveToNext())
    {
        String s=res.getColumnName(3);
        if(s=="male")
        {
            imageView.setImageResource( images.get( "male").intValue() );
        }
        else  
        {
            imageView.setImageResource( images.get( "female" ).intValue() ); 
        }
    }
    if (convertView == null) 
    {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } 
    else 
    {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}   
// references to our images
private Integer[] mThumbIds = {R.drawable.add,};

1 个答案:

答案 0 :(得分:2)

rawQuery语句后面的代码中放入以下行

res.moveToFirst()