如何在全局变量中获取List <sting>的数据以在Oncreate()方法中获取数据?

时间:2018-10-26 05:59:04

标签: android android-debug android-developer-api arcgis-android-api

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

    }}

当我尝试从方法 getRetrofitPostHospitalObject()中的 PostData 类访问的数据在我调用时在 MainActivity 中的变量“ List gobalAr”中它在gobalAr中显示 null 值,正如我从 gobalArr 中插入数据的地方,其中添加 getRetrofitPostHospitalObject() >,请提供帮助。

实际上,我想将PostData类的数据“ gobalAr”变量访问到MainActivity。

2 个答案:

答案 0 :(得分:0)

为List创建get()方法,就像这样

public List<String> getList(){
    return *****; 
  } 

答案 1 :(得分:0)

简便的解决方案在MainActivity中调用此方法

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
}

然后在内部进行其余的操作。问题解决了