如何通过gridview位置拉取图像

时间:2015-07-05 13:13:58

标签: android gridview

一切正常,只是图片没有显示

BaseAdapter:

public Bitmap getItem(int position) {
    return bitmaps.get(position);
}

的活动:

Intent i = new Intent(getApplicationContext(), SpcifiesImage.class);
                    // passing array index
                    i.putExtra("id", position);`enter code here`
                    startActivity(i);

这是我希望图像的活动:

Intent intent = new Intent(getApplicationContext(), SpcifiesImage.class);

    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    CustomGrid imageAdapter = new CustomGrid(mContext);

    ImageView imageView = (ImageView) findViewById(R.id.imageView3);
    imageView.setImageBitmap(imageAdapter.bitmaps.get(position));


    // passing array index
    intent.putExtra("mas", massage);

    startActivity(intent);

CustomGrid:

         Context mContext;
private Bitmap btimaprecieve;
List<ParseObject> ParseObjects;
ArrayList<Bitmap> bitmaps;
ParseQuery<ParseObject> query;
ProgressDialog progressDialog;
int size;


public CustomGrid(Context c, ArrayList<Bitmap> bitmaps) {
    mContext = c;
    this.bitmaps = bitmaps;

}

public CustomGrid(Context mContext) {
}


//---returns the number of images---
public int getCount() {
    return bitmaps.size();
}

//---returns the ID of an item---
public Bitmap getItem(int position) {
    return bitmaps.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(mContext);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.mobile, null);

        // set value into textview
       // TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
      //  textView.setText(mobileValues[position]);

        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);
       // imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
       // imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageBitmap(bitmaps.get(position));


    } else {
        gridView = (View) convertView;
    }
    return gridView;

logcat的:

进程:com.example.tepper.myapplication,PID:16637     java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream)'

1 个答案:

答案 0 :(得分:0)

好的,从您发布的代码中,您对此部分有疑问:

在CustomGrid上:

public CustomGrid(Context mContext) {
}

构造函数为空,表示执行此操作时:

CustomGrid imageAdapter = new CustomGrid(mContext); 

ImageView imageView = (ImageView) findViewById(R.id.imageView3);
imageView.setImageBitmap(imageAdapter.bitmaps.get(position));

bitmaps数组不会被初始化。 您必须使Bitmap数组静态或将其存储在可以从另一个活动访问的位置,只是为了测试目的:

  • 在CustomGrid上更改以下行:

    ArrayList<Bitmap> bitmaps;public static ArrayList<Bitmap> bitmaps;

  • 然后在下一个活动中访问它:

    imageView.setImageBitmap(CustomGrid.bitmaps.get(position));