Home » Android » Recyclview showing data while debug mode but not working in noraml run

Recyclview showing data while debug mode but not working in noraml run

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am working on android application and I am facing one problem. When I run my app then nothing showing in recycle view but when i check application in debug mode then data successfully showing in recycle view. I have checked my web service working fine and successfully giving data. How can i achieve this ?

ManageQuestionActivity,java

public class ManageQuestionActivity extends AppCompatActivity implements RecyclerView.OnScrollChangeListener{

    private static final String TAG = MainActivity.class.getSimpleName();
    private RecyclerView listView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;
    private QuestionsListAdapter listAdapter;
    private List<QuestionsItem> timeLineItems;
    private int requestCount = 1;
    private ProgressDialog pDialog;
    public static String id, message, token, encodedString;
    int pageCount, totalPages;
    SQLiteHandler db;
    SessionManager session;
    ConnectionDetector cd;
    EditText edtSearch;
    Boolean isInternetPresent = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_question);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        listView = (RecyclerView) findViewById(R.id.list);
        edtSearch = (EditText) findViewById(R.id.edtSearch);
        listView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        listView.setLayoutManager(layoutManager);
        //Adding an scroll change listener to recyclerview
        listView.setOnScrollChangeListener(this);

        // Progress dialog
        pDialog = new ProgressDialog(this);
        pDialog.setCancelable(false);

        cd = new ConnectionDetector(this);
        isInternetPresent = cd.isConnectingToInternet();

        db = new SQLiteHandler(this);

        // session manager
        session = new SessionManager(this);

        // Fetching user details from sqlite
        HashMap<String, String> user = db.getUserDetails();
        id = user.get("id");
        token = user.get("token");

        getData();

        timeLineItems = new ArrayList<>();

        adapter = new QuestionsListAdapter(timeLineItems, this);
        listView.setAdapter(adapter);

    }

    public void getTimeLineData(final String token, final String page) {


        String tag_string_req = "req_register";
        // making fresh volley request and getting json
        StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.questions, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                VolleyLog.d(TAG, "Response: " + response.toString());
                if (response != null) {
                    try {
                        JSONObject jObj = new JSONObject(response);
                        boolean error = jObj.getBoolean("status");
                        String message = jObj.getString("message");
                        if (error) {
                            totalPages = jObj.getInt("totalPages");
                            pageCount = jObj.getInt("page");

                            int limit = jObj.getInt("limit");
                            parseJsonFeed(response);
                        }

                    } catch (Exception e) {

                    }

                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("my_token", token);
                params.put("page", page);
                params.put("limit", "20");

                return params;
            }
        };

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

    }

    private void parseJsonFeed(String response) {
        try {
            JSONObject jsonObj = new JSONObject(response);
            JSONArray feedArray = jsonObj.getJSONArray("data");
            for (int i = 0; i < feedArray.length(); i++) {
                JSONObject feedObj = (JSONObject) feedArray.get(i);

                QuestionsItem item = new QuestionsItem();
                item.setId(feedObj.getInt("id"));
                item.setQuestion(feedObj.getString("question"));
                String options = feedObj.getString("multi_ans_option");
                String[] parts = options.split("\|");
                String part1 = parts[0];
                String part2 = parts[1];
                String part3 = parts[2];
                String part4 = parts[3];
                item.setAnsOne(part1);
                item.setAnsTwo(part2);
                item.setAnsThree(part3);
                item.setAnsFour(part4);
                item.setAnswer(feedObj.getString("answer"));
                timeLineItems.add(item);
            }

            // notify data changes to list adapater
            adapter.notifyDataSetChanged();


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getData() {
        //Adding the method to the queue by calling the method getDataFromServer
        getTimeLineData(token, String.valueOf(requestCount));
        //Incrementing the request counter
        requestCount++;
    }

    //This method would check that the recyclerview scroll has reached the bottom or not
    private boolean isLastItemDisplaying(RecyclerView recyclerView) {
        if (recyclerView.getAdapter().getItemCount() != 0) {
            int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
            if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)

                return true;
        }
        return false;
    }

    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        //Ifscrolled at last then
        if (isLastItemDisplaying(listView)) {
            //Calling the method getdata again
            getData();
        }
    }
}

activity_manage_question.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@null" />

</LinearLayout>

QuestionsItem.java

public class QuestionsItem {
    private int id;
    private String question, ansOne, ansTwo, ansThree, ansFour, answer;

    public QuestionsItem() {
    }

    public QuestionsItem(int id, String question, String ansOne, String ansTwo, String ansThree, String ansFour, String answer) {
        super();
        this.id = id;
        this.question = question;
        this.ansOne = ansOne;
        this.ansTwo = ansTwo;
        this.ansThree = ansThree;
        this.ansFour = ansFour;
        this.answer = answer;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getAnsOne() {
        return ansOne;
    }

    public void setAnsOne(String ansOne) {
        this.ansOne = ansOne;
    }

    public String getAnsTwo() {
        return ansTwo;
    }

    public void setAnsTwo(String ansTwo) {
        this.ansTwo = ansTwo;
    }

    public String getAnsThree() {
        return ansThree;
    }

    public void setAnsThree(String ansThree) {
        this.ansThree = ansThree;
    }

    public String getAnsFour() {
        return ansFour;
    }

    public void setAnsFour(String ansFour) {
        this.ansFour = ansFour;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

QuestionsListAdapter.java

questios_item.xml

Answers:

listView.setAdapter should not be here cut

And paste in this method: