Question

I am trying to get the image URL from the selected image that the user selects so that I can then pass that string/URL to another Fragment that I have set up so that the image can be viewed fully via an image view.

How would I be able to achieve this? thanks.

Here is the SampleGridViewAdapter class:

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

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

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



}

@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() //
            .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;
}
}

Here is my SampleGridViewActivity:

public class SampleGridViewActivity extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.sample_gridview_activity,
            container, false);

    GridView Grid = (GridView) view.findViewById(R.id.grid_view);
    Grid.setAdapter(new SampleGridViewAdapter(getActivity()));

    Grid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long urls) {
            // TODO Auto-generated method stub

            FragmentManager fm = getFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();

            Fragment openImage = new ImageViewPager();

            transaction.replace(R.id.content_frame, openImage);

            transaction.addToBackStack(null);

            transaction.commit();

        }
    });

    return view;

}

 }

Here is the class I would like to send the string to:

public class ImageViewer extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.imageviewpager, container, false);

    ImageView IMGView = (ImageView) view.findViewById(R.id.imageView1);

    Picasso.with(getActivity()).load("Pass URL into HERE").into(IMGView);

    return view;
}

}
Was it helpful?

Solution

You should use Argument feature of Fragments.

When you create your Fragment, you can pass the URL as an Argument

Fragment openImage = new ImageViewPager();
Bundle b = new Bundle();
b.putString("url", DATA.URLs.get(position));
openImage.setArguments(b);
transaction.replace(R.id.content_frame, openImage);

And then when your Fragment is inflated you can get that Argument string

Picasso.with(getActivity()).load(getArguments().getString("url")).into(IMGView);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top