PostData.java
public class PostData {
List<String> gobalAr=new ArrayList<String>();
public List<String> getList(){
return gobalAr;
}
public void setList( List<String> gl){
this.gobalAr = gl;
}
String url = "http://btownmedia.com/";
ArrayList<Post> postArry = new ArrayList();
public List<String> bankin = new ArrayList<String>();
public void getRetrofitPostHospitalObject() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitObjectAPI service = retrofit.create(RetrofitObjectAPI.class);
Call<PostArtical> call = service.getPostHospitalDetails();
call.enqueue(new Callback<PostArtical>() {
@Override
public void onResponse(Response<PostArtical> response, Retrofit retrofit) {
try {
List<Post> PostData = response.body().getPosts();
for (int i = 0; i < PostData.size(); i++) {
Post p = new Post();
p.setId(PostData.get(i).getId());
p.setTitle(PostData.get(i).getTitle());
p.setExcerpt(PostData.get(i).getExcerpt());
p.setThumbnail(PostData.get(i).getThumbnail());
postArry.add(p);
}
gobalAr=new ArrayList<String>();
for (int i = 0; i < postArry.size(); i++) {
Post c = new Post();
c=postArry.get(i);
gobalAr.add(c.getTitle());
}
setList(gobalAr);
Log.d("valuein",gobalAr.toString()); //[Birtamode Eye Hospital (P) Ltd., Birta City Hospital, Life Line Hospital]
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
Log.d("valueout",gobalAr.toString()); // null
}}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PostData postData = new PostData();
postData.getRetrofitPostHospitalObject();
List<String> list = postData.getList();
Log.d("On", list.toString()); //null
}}
当我尝试访问来自MainActivity中的方法 getRetrofitPostHospitalObject() 变量“List gobalAr”的PostData类的数据时,当我调用它时,它在 gobalAr 中显示空值,因为我已经从gobalArr插入数据,其中添加了 getRetrofitPostHospitalObject(),请帮忙..
实际上,我想将 PostData 类的数据“gobalAr”变量访问到 MainActivity。