質問

I'm using Picasso to set a gridview in my Android app. What I wanna do is basically pressing an image in the gridview and go to the detail view.

The data was populated in String array like so:

static final String[] URLS = {
   "http://i.imgur.com/file1.jpg", "http://i.imgur.com/file2.jpg",
   "http://i.imgur.com/file3.jpg", "http://i.imgur.com/file4.jpg"
};

In the ImageAdapter those URLS are to be put in an String ArrayList:

public class ImageAdapter extends BaseAdapter {
  private final Context context;
  private final List<String> urls = new ArrayList<String>();

  public ImageAdapter(Context context) {
    this.context = context;

    // Ensure we get a different ordering of images on each run.
    Collections.addAll(urls, Data.URLS);
    //Collections.shuffle(urls);

    // Triple up the list.
    ArrayList<String> copy = new ArrayList<String>(urls);
    urls.addAll(copy);
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    SquaredImageView view = (SquaredImageView) convertView;
    if (view == null) {
      view = new SquaredImageView(context);
      view.setScaleType(CENTER_CROP);
    }

    // Get the image URL for the current position.
    String url = getItem(position);

    // Trigger the download of the URL asynchronously into the image view.
    Picasso.with(context) //
        .load(url) //
        .placeholder(R.drawable.placeholder) //
        .error(R.drawable.error) //
        .fit()
        .centerCrop()//
        .into(view);

    return view;
  }

  @Override public int getCount() {
    return urls.size();
  }

  @Override public String getItem(int position) {
    return urls.get(position);
  }

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

}

The problem is when I want to get the Item's ID, it was all stored in String. Here is how my Details View Activity looks like:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter ia = new ImageAdapter(this);

    ImageView iv = (ImageView) findViewById(R.id.full_image_view);
    iv.setImageResource(ia.getItemId(position));
}

The position is from my main activity and it is in Integer. When I want to put the getItemId, setImageResource throws this error:

The method setImageResource(int) in the type ImageView is not applicable for the arguments (long)

Do you guys have any idea how to fix this?

EDIT: Added ImageAdapter class code

役に立ちましたか?

解決

You still need to call Picasso in the detail view and pass the URL from the adapter, not the ID.

Picasso.with(context) //
    .load(ia.getItem(position)) //
    .placeholder(R.drawable.placeholder) //
    .error(R.drawable.error) //
    .fit()
    .centerCrop()//
    .into(iv);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top