简体   繁体   English

Adapter.notifyDataSetChanged()在onCreate()中不起作用

[英]Adapter.notifyDataSetChanged() not working inside onCreate()

I am trying to parse JSON using Volley. 我正在尝试使用Volley解析JSON。

I am using a vector to store parsed data and calling adapter.notifyDataSetChanged() inside onCreate() after filling vector. 我正在使用向量存储解析的数据,并在填充向量后在onCreate()内调用adapter.notifyDataSetChanged() But no changes are there. 但是没有变化。

If I am calling adapter.notifyDataSetChanged() inside try-catch block of getData() then it is working fine. 如果我在getData() try-catch块内调用adapter.notifyDataSetChanged() ,则它工作正常。 Why? 为什么?

public class MainActivity extends AppCompatActivity{

    Vector<Data> ve;
    private  Adapter adapter;
    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=(RecyclerView)findViewById(R.id.main_recycler_view);
        ve=new Vector<Data>();
        adapter=new Adapter(ve);
        RecyclerView.LayoutManager lm=new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(lm);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        getData(ve);
        adapter.notifyDataSetChanged();  //not working
    }
    public void getData(final Vector<Data> ve)
    {
        String url = "https://api.androidhive.info/contacts/";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String jsonString) {
                try
                {
                    JSONObject object = new JSONObject(jsonString);
                    JSONArray arr = object.getJSONArray("contacts");
                    int len=arr.length();
                    JSONObject obj;
                    for (int i = 0; i < len; i++)
                    {
                        obj = arr.getJSONObject(i);
                        ve.add(new Data(obj.getString("name"), obj.getString("email"));

                    }
                   //adapter.notifyDataSetChanged();working
                }
                catch (JSONException e)
                {
                    Toast.makeText(getApplicationContext(),"JSON Parsing Exception",Toast.LENGTH_LONG);
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "Some error occurred!!", Toast.LENGTH_SHORT).show();

            }
        });
        RequestQueue rQueue = Volley.newRequestQueue(this);
        rQueue.add(request);

    }

}

you cannot do that reason nothing volley used network requests which are designed to be asynchronous in nature , so the network request is put in queue and code continues to execute the notifyadapter changed method . 您无法执行此操作,原因是Volley没有使用本质上设计为异步的网络请求,因此网络请求被放入队列中,代码继续执行notifyadapter更改的方法。 The response to the network call occurs afterwards and add data, you should call that method after data is received by calling it inside volley on response method 之后会发生对网络调用的响应并添加数据,您应该在接收到数据后通过在volley on响应方法中调用它来调用该方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM