【问题标题】:not able to access values of array list from mainactivity无法从 mainactivity 访问数组列表的值
【发布时间】:2025-11-22 22:45:01
【问题描述】:

我有一个程序,它使用 Facebook Graph API 从 Facebook 图像中获取各种帖子的帖子 ID。这是我为此使用的代码:

oncreate()

public class MainActivity extends AppCompatActivity {

    //Variable Declarations
    TextView textView1;
    public  ArrayList<String> postIds = new ArrayList<>();//array to store various post ids
    ArrayList<String> imageIds = new ArrayList<>();//has the various imageURLs
    ImageView img1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getPosts();
        System.out.println("PostIDS value in Main Activity:"+postIds.toString());
        System.out.println("Done Execution From Main Activity");

    }//onCreate

Getposts():

public void getPosts()
    {
        GraphRequest request = GraphRequest.newGraphPathRequest(
                a,
                "/internationalchaluunion",
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse response) {
                        // Insert your code here
                        System.out.println("Response::" + String.valueOf(response.getJSONObject()));
                        JSONObject jsRoot = response.getJSONObject();
                        JSONObject jsArr1 = null;
                        try {
                            jsArr1 = jsRoot.getJSONObject("posts");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        JSONArray arr1 = jsArr1.optJSONArray("data");

                        //iterate JSONArray and store the values to different
                        for(int i=0; i < arr1.length(); i++)
                        {
                            try {
                                JSONObject jsonObject = arr1.getJSONObject(i);
                                //System.out.println("Id"+i+":"+jsonObject.optString("id"));
                                String str = jsonObject.optString("id");
                                str=str.substring(str.indexOf("_")+1,str.length());
                                postIds.add(str);
                            }//try
                            catch (JSONException e) {
                                e.printStackTrace();
                            }//catch


                        }//for

                        /
                }//onCompletedMethod
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "posts");
        request.setParameters(parameters);
        request.executeAsync();


    }

当我尝试在 getposts() 函数中访问 postIDs 的值时,我可以看到所有值都已正确设置。但是当我尝试从 main 方法或任何其他方法访问 postsIds[] 时是同一个类postIDs[] 是一个空数组。你能告诉我我在这里缺少什么吗?

【问题讨论】:

  • 在尝试从其他方法访问请求之前,您是否确保请求已完成?

标签: android json facebook arraylist


【解决方案1】:

查看您的代码,我们可以确认您正在执行异步调用以获取 getPosts();

中所需的数据

尽可能在 onComplete() 回调中使用获取的数据是一种很好的做法,因为您永远不知道异步任务将执行多长时间 - 这取决于如果在内部存在,则在网络/处理器/重计算上。

在 Android 中检查这个 example generic AsyncTask

【讨论】: