Search code examples
javaandroidmethodsretrofit2reusability

Retrofit method response reusing into another activity


How can I get the data from getDataForId(Integer.toString(1)); by calling the same getDataForId method from the DisplayData class?

I want reuse the same method and get the result. It doesn't make sense to copy and paste the same method into the other activity class. Then there will be the same code repeated twice.

This is my DisplayData.class

  public class DisplayData extends AppCompatActivity {

    Detail reqDetail;
    String BASE_URL = "";
    TextView name;

    ImageView image;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_data);

        name = (TextView) findViewById(R.id.name);

        image = (ImageView)findViewById(R.id.image);



        public void getDataForId(final String id) {

        ApiInterface apiInterface = APIClient.getApiInterface();
        Call<MyResponse> call = apiInterface.getResponse();
        call.enqueue(new Callback<MyResponse>() {

        @Override
        public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {

               if (response.body() != null) {
                MyResponse myResponse = response.body();
                    List<Detail> details = myResponse.getDetails();
                        for (Detail d : details) {
                            if (d.getId().equals(id)) {
                                reqDetail = d;
                                    name.setText(reqDetail.getName());

                                            Picasso.with(DisplayData.this)
                                            .load(reqDetail.getName())
                                            .placeholder(R.mipmap.ic_launcher)
                                            .error(R.mipmap.ic_launcher)
                                            .into(image);
                                            }
                                        }

                                    }
                                }

            @Override
            public void onFailure(Call<MyResponse> call, Throwable t) {

            }
        });
    }

This is my SecondData class where I want to display the same data response of DisplayData by reusing same methods

public class SecondData extends AppCompatActivity {

    Detail reqDetail;
    String BASE_URL = "";
    TextView name;

    ImageView image;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_main);

        name = (TextView) findViewById(R.id.name);

        image = (ImageView)findViewById(R.id.image);



    }


}

Solution

  • Create a class to make retrofit call like this

    public class SampleClass {
    private DataInterface mListener;
    
    public SampleClass() {
        super();
    }
    
    public void getDataForId(final String id) {
        ApiInterface apiInterface = APIClient.getApiInterface();
        Call<MyResponse> call = apiInterface.getResponse();
        call.enqueue(new Callback<MyResponse>() {
            @Override
            public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                if (response!=null && response.body() != null && mListener != null) {
                    mListener.responseData(response.body());
                }
            }
            @Override
            public void onFailure(Call<MyResponse> call, Throwable t) {
    
            }
        });
    }
    
    public void setOnDataListener(DataInterface listener) {
        mListener = listener;
    }
    
    public interface DataInterface {
        void responseData( MyResponse myResponse );
    }
    }
    

    And in your activity just call the class like this

    SampleClass sampleClass = new SampleClass();
    sampleClass.setOnDataListener(new SampleClass.DataInterface() {
        @Override
        public void responseData(MyResponse myResponse) {
    
        }
     });
    sampleClass.getDataForId("UR ID");
    

    Also in your class store the ID as private memeber variable

    private Integer YOUR_ID;
    

    Then on getting the result compare the result ID with this ID

      List<Detail> details = myResponse.getDetails();
        for (Detail d : details) {
            if (d.getId().equals(YOUR_ID)) {
                reqDetail = d;
                name.setText(reqDetail.getName());
    
                Picasso.with(DisplayData.this)
                        .load(reqDetail.getName())
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .into(image);
              }
        }