RecyclerView不会立即显示任何内容

时间:2017-06-26 15:38:31

标签: android android-recyclerview android-volley

我正在尝试解决与RecyclerView相关的问题。当我第一次尝试显示对象列表时,我什么也看不见。由于已知为异步的截击请求,数据将被传递。所以,我尝试过像onDataSetChanged()这样的各种解决方案,但没有任何反应。我想说的是解决方案有效,第二次看列表时我可以毫无问题地看到它。

这是我的代码:

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;

private static MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(false);

    recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
    adapter = new MyAdapter(this,initData());
    adapter.setParentClickableViewAnimationDefaultDuration();
    adapter.setParentAndIconExpandOnClick(true);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
}

private List<ParentObject> initData() {
    TitleCreator titleCreator = TitleCreator.get(this);
    List<TitleParent> titles = titleCreator.getAll();
    List<ParentObject> parentObject = new ArrayList<>();

    for(TitleParent title:titles)
    {
        List<Object> childList = getList(title);
        title.setChildObjectList(childList);
        parentObject.add(title);
    }

    return parentObject;
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    ((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}

private List<Object> getList(final TitleParent title){
    final List<Object> childList = new ArrayList<>();

    // Tag used to cancel the request
    String tag_string_req = "request";

    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_DASHBOARD, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d("POL", "Response: " + response.toString());

            try {
                JSONObject jObj = new JSONObject(response);

                for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
                    JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
                    String certificate = user.getString("numero_contratto");
                    if(title.getTitle().contains(certificate)){
                        String scadenza = user.getString("scadenza");
                        String totale = user.getString("totale");
                        childList.add(new TitleChild(scadenza,totale));
                    }
                }
                Log.d("TRY", "Response: " + response.toString());
                adapter.notifyDataSetChanged();
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                //  Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("DASH", "Login Error: " + error.getMessage());
            //Toast.makeText(getApplicationContext(),
            //       error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

    return childList;
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

有几点

  1. 获取列表方法在发出截击请求后返回空列表。
  2. 确保来自服务器的列表填充数据然后调用设置数据。
  3. 你必须尝试下面的东西。

    public class MainActivity extends AppCompatActivity {
    
    RecyclerView recyclerView;
    
    private static MyAdapter adapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);
    
        recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
        // show some loading bar
         initData();
    
    }
    
      private void initData() {
        TitleCreator titleCreator = TitleCreator.get(this);
        List<TitleParent> titles = titleCreator.getAll();
        List<ParentObject> parentObject = new ArrayList<>();
    
        for(TitleParent title:titles)
        {
            List<Object> childList = getList(title);
            title.setChildObjectList(childList);
            parentObject.add(title);
        }
    
    
    }
     private void setData(List<ParentObject> parentObject) {
     // load the data here            
        adapter = new MyAdapter(this,parentObject);
                adapter.setParentClickableViewAnimationDefaultDuration();
    adapter.setParentAndIconExpandOnClick(true);
    
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
    }
    
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
    }
    
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.left_enter, R.anim.right_out);
    }
    
    private List<Object> getList(final TitleParent title){
        final List<Object> childList = new ArrayList<>();
    
        // Tag used to cancel the request
        String tag_string_req = "request";
    
        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_DASHBOARD, new Response.Listener<String>() {
    
            @Override
            public void onResponse(String response) {
                Log.d("POL", "Response: " + response.toString());
    
                try {
                    JSONObject jObj = new JSONObject(response);
    
                    for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
                        JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
                        String certificate = user.getString("numero_contratto");
                        if(title.getTitle().contains(certificate)){
                            String scadenza = user.getString("scadenza");
                            String totale = user.getString("totale");
                            childList.add(new TitleChild(scadenza,totale));
                        }
                     setData(childList);
                    }
                    Log.d("TRY", "Response: " + response.toString());
    
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                    //  Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("DASH", "Login Error: " + error.getMessage());
                //Toast.makeText(getApplicationContext(),
                //       error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    
        return childList;
    }