当我点击图片时,如何从gridview列表中获取图像,我使用的是毕加索库

时间:2014-11-23 07:53:26

标签: android gridview android-intent picasso

我想当我从gridview点击图片时,图片会发送到新版面(详情图片)

这是MainActivity

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Intent i = new  Intent(getApplicationContext(), SingleViewActivity.class);
            i.putExtra("arg3", arg2);
            startActivity(i);
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "okey", Toast.LENGTH_SHORT).show();
        }
    });

}

ImageAdapter

public class ImageAdapter extends BaseAdapter {
public static final String URL ="http://api.androidhive.info/json/movies/";
Context mContext;
int mThumbIds = 18;
public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
     return mThumbIds;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(95, 95));
   //     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(4, 4, 4, 4);
    } else {
        imageView = (ImageView) convertView;
    }

    Picasso.with(this.mContext)
    .load(URL + position +".jpg")
  //  .placeholder(R.drawable.loader).error(R.drawable.ic_launcher).fit()
   .into(imageView);


  //  imageView.setImageResource(mThumbIds[position]);
   return imageView;
    }
// references to our images }

在这里我尝试从gridview获取图像,我尝试使用intent,它不起作用

详情图片

public class SingleViewActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_view);
    Intent i = getIntent();
    int position = i.getExtras().getInt("arg3");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.SingleView);


    imageView.setImageResource(imageAdapter.getItemViewType(position));}}

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

两点:

  • 将您从MainActivity中选择的图像的URL传递给SingleViewActivity。
  • 使用ImageLoader,不要修改宽度/高度,否则内存缓存将会丢失。

其他缓存会为你做一切。