Retrofit method response reusing into another activity

Viewed 2407

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);



    }


}
3 Answers

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);
          }
    }

You should create a BaseActivity and extend the other two from it. Then, you place the method you want to use in both of them in the BaseActivity and make it public so you can use it properly.

Note that anything you need in your method from outside it won't be available in the BaseActivity, so you should either pass in the constructor or declare it in the BaseActivity if possible.

You should also think on how the return of your method should be for you to have access to its results.

You can refer to this question to learn more:

trying not to repeat myself (android/java)

So, If you want to use response of one Activity to another Activity,

Then, First save response to List or ArrayList.

And Then Pass data Using Intent.

That should do a trick.

Related