0

I'm trying to make a list of images with a gridview which when the user selects one of the images will display the selected image in fullscreen. What I want to ask is how to know the position of the selected image and notify the image's position to the intent so that it can be displayed in fullscreen?

package com.example.bpskotayogyakarta.ui.infografis;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.example.bpskotayogyakarta.R;
import com.google.gson.annotations.SerializedName;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public class InfografisFragment extends Fragment {

    private InfografisViewModel infografisViewModel;
    private List<Spacecraft> spacecraftList;

    private GridViewAdapter adapter;
    private GridView gridView;
    ProgressBar myProgressBar;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        infografisViewModel = ViewModelProviders.of(this).get(InfografisViewModel.class);
        View root = inflater.inflate(R.layout.fragment_infografis, container, false);

        final ProgressBar myProgressBar= root.findViewById(R.id.progressBar2);
        myProgressBar.setVisibility(View.VISIBLE);

        MyAPIService myAPIService = RetrofitClientInstance.getRetrofitInstance().create(MyAPIService.class);
        gridView = root.findViewById(R.id.grid_view);

        Call<List<Spacecraft>> call = myAPIService.getSpacecrafts();
        call.enqueue(new Callback<List<Spacecraft>>() {

            @Override
            public void onResponse(Call<List<Spacecraft>> call, Response<List<Spacecraft>> response) {
                myProgressBar.setVisibility(View.GONE);
                spacecraftList = response.body();
                adapter = new GridViewAdapter(getContext(), spacecraftList);
                gridView.setAdapter(adapter);
            }
            @Override
            public void onFailure(Call<List<Spacecraft>> call, Throwable throwable) {
                myProgressBar.setVisibility(View.GONE);
                Toast.makeText(getContext(), throwable.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

        return root;
    }

    class Spacecraft {
        /*
        INSTANCE FIELDS
         */
        @SerializedName("id_info")
        private int id_info;

        @SerializedName("gambar_info")
        private String gambar_info;

        public Spacecraft(int id_info, String gambar_info) {
            this.id_info = id_info;
            this.gambar_info = gambar_info;
        }

        /*
         *GETTERS AND SETTERS
         */
        public int getId() {
            return id_info;
        }
        public void setId(int id_info) {
            this.id_info = id_info;
        }

        public String getImageURL() {
            return gambar_info;
        }
    }

    interface MyAPIService {

        @GET("connection_image_bps.php")
        Call<List<Spacecraft>> getSpacecrafts();
    }

    static class RetrofitClientInstance {

        private static Retrofit retrofit;
        private static final String BASE_URL = "https://bpskotayogyakarta.000webhostapp.com/";

        public static Retrofit getRetrofitInstance() {
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        }
    }

    class GridViewAdapter extends BaseAdapter {

        private List<Spacecraft> spacecrafts;
        private Context context;

        public GridViewAdapter(Context context,List<Spacecraft> spacecrafts){
            this.context = context;
            this.spacecrafts = spacecrafts;
        }

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

        @Override
        public Object getItem(int position) {
            return spacecrafts.get(position);
        }

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

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            if(view==null)
            {
                view=LayoutInflater.from(context).inflate(R.layout.list_gambar_info,viewGroup,false);
            }

            ImageView spacecraftImageView = view.findViewById(R.id.image_view);
            spacecraftImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            final Spacecraft thisSpacecraft= spacecrafts.get(position);

            if(thisSpacecraft.getImageURL() != null && thisSpacecraft.getImageURL().length()>0)
            {
                Picasso.get().load(thisSpacecraft.getImageURL()).placeholder(R.drawable.placeholder).into(spacecraftImageView);
            }else {
                Toast.makeText(context, "Empty Image URL", Toast.LENGTH_LONG).show();
                Picasso.get().load(R.drawable.placeholder).into(spacecraftImageView);
            }

            view.setOnClickListener(new View.OnClickListener() {

                private int id_temp = thisSpacecraft.getId();

                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, FullscreenInfo.class);
                    intent.putExtra("image", String.valueOf(thisSpacecraft)); // put image data in Intent
                    context.startActivity(intent);
                }
            });

            return view;
        }
    }
}

And below is the code that I intend to send to

package com.example.bpskotayogyakarta.ui.infografis;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.bpskotayogyakarta.R;

public class FullscreenInfo extends AppCompatActivity {
    ImageView selectedImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fullscreen_info);
        selectedImage = (ImageView) findViewById(R.id.fullscreen_view_image); // init a ImageView
        Intent intent = getIntent(); // get Intent which we set from Previous Activity
        selectedImage.setImageResource(intent.getIntExtra("image", 0)); // get image from Intent and set it in ImageView
    }
}

So what's wrong with my code?

1 Answers1

0

You can use onItemClickListener for that

   // Implement On Item click listener
gridView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       Spacecraft spaceCraft = (Spacecraft)parent.getAdapter().getItem(position);
       Intent intent = new Intent(getActivity,FullscreenInfo.class);
       intent.putExtra("image_url",spaceCraft.getImageURL());
       startActivity(intent);

    }
}); 


and inside your fullscreenInfo you can set your image like this

public class FullscreenInfo extends AppCompatActivity {
ImageView selectedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen_info);
    selectedImage = (ImageView) findViewById(R.id.fullscreen_view_image); // init a ImageView
     String imageUrl = getIntent().getStringExtra("image_url");
      //you can use this image url to load image in your imageview by Using any library like picasso or Glide
}

}

Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22
  • hello can you help me out here --> https://stackoverflow.com/questions/59532667/particular-titlefetched-from-api-using-searchview – Wini Jan 03 '20 at 09:16