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