BigMeister
BigMeister

Reputation: 371

RecyclerView does not show anything immediately

I'm trying to fix a problem related to RecyclerView. When I try to show the list of objects for the first time I don't see anything. The data are passed thanks to a volley request that is known to be async. So, I've tried various solutions like onDataSetChanged() but nothing happens. I want to remark that the solution works, the second time I watch the list I can see it without any problems.

Here's my code:

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;
}

Thank you for your help!

Upvotes: 1

Views: 943

Answers (1)

Sush
Sush

Reputation: 3874

well there are couple of points

  1. get list method returning empty list after made volley request.
  2. make sure list filled data from the server then call the set data.

you have to try something like below.

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;
}

Upvotes: 1

Related Questions