0

In my Application I want to retrieve a data from the database. But the problem I am facing is that, the data is fetched from database but it is not displaying at a time when I reopen the page at that time the data is displaying. I want to reload a page when I click on Button.

Layout

Here the code is as follow :-

Btngetdata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             new InTimeInsert().execute();
        }
    });


private class InTimeInsert extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... args) {

            try {
                arraylist = new ArrayList<HashMap<String, String>>();

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("at_username", uid));

                JSONObject json = jParser.makeHttpRequest(url_intime,"GET", params);

                //ownerObj = json.getJSONArray("visit");
                for (int i = 0; i < ownerObj.length(); i++) {
                    jsonobject = ownerObj.getJSONObject(i);
                    time_fetch.add(jsonobject.getString("at_itime"));
                }
            } catch (Exception e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void args) {
            ina.setText(""+delivery_fetch);
        }
    }



private class AllAtendence extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... args) {

        try {
            arraylist = new ArrayList<HashMap<String, String>>();

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("at_username", uid));

            JSONObject json = jParser.makeHttpRequest(url_allatendence,"GET", params);

            ownerObj = json.getJSONArray("visit");
            for (int i = 0; i < ownerObj.length(); i++) {
                jsonobject = ownerObj.getJSONObject(i);

                delivery_fetch =jsonobject.getString("at_date");
                lunch=jsonobject.getString("at_litime");
                rejoin=jsonobject.getString("at_lotime");
                out=jsonobject.getString("at_otime");
                Log.d("at_line",json.toString());

            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void args) {
        ina.setText(""+delivery_fetch);
        rejoina.setText(""+lunch);
        luncha.setText(""+rejoin);
        outa.setText(""+out);
        if(ina.getText().toString().equals(""))
        {
            Btngetdata.setVisibility(View.VISIBLE);
            inti.setVisibility(View.GONE);

        }
        else
        {
            Btngetdata.setVisibility(View.GONE);
        }
        if(luncha.getText().toString().equals(""))
        {
            ltime.setVisibility(View.VISIBLE);
            luncht.setVisibility(View.GONE);
        }
        else
        {
            ltime.setVisibility(View.GONE);
        }
        if(rejoina.getText().toString().equals(""))
        {
            rtime.setVisibility(View.VISIBLE);
            rejoint.setVisibility(View.GONE);
            }
        else
        {
            rtime.setVisibility(View.GONE);
        }
        if(outa.getText().toString().equals(""))
        {
            otime.setVisibility(View.VISIBLE);
            outt.setVisibility(View.GONE);
        }
        else
        {
            otime.setVisibility(View.GONE);
        }


    }
}   
  • If your are using `Activity` you can restart that `Activity` without giving `No Animation` to it. – Harshad Pansuriya Dec 15 '16 at 05:33
  • I am using Fragment. –  Dec 15 '16 at 05:35
  • Then you have to `detach` the `Fragment` and `Reattch` to it. – Harshad Pansuriya Dec 15 '16 at 05:36
  • Can you please explain me with some code??? –  Dec 15 '16 at 05:36
  • Possible duplicate of [refresh fragment at reload](http://stackoverflow.com/questions/20702333/refresh-fragment-at-reload) – Harshad Pansuriya Dec 15 '16 at 05:37
  • You fetching some data ... to display in some UI ........ why you need to reload page ... can you tell me purpose ... is there another UI depend or need to update on your data which already have some data? –  Dec 15 '16 at 05:57
  • @EnamulHaque I am fetching the Current time from the APi and when i clicked on Button it will generate and insert the current time onDB and fetching the currnet time in the TextView –  Dec 15 '16 at 06:02
  • Your 'time_fetch' is like this, ArrayList time_fetch = new ArrayList();............right? New r you sure 'delivery_fetch' is perfectly assigned data form your 'time_fetch' object? –  Dec 15 '16 at 06:09
  • yes... should i upadate my question and upload my whole code?? –  Dec 15 '16 at 06:12
  • ArrayList time_fetch = new ArrayList(); my time_fetch is like this –  Dec 15 '16 at 06:34

3 Answers3

0
//put your code in onResume methods
    @Override protected void onResume() {
        super.onResume();
       // call here
        new InTimeInsert().execute();
    }
Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
0

Add .get();, while calling AsyncTask.

Like:

new InTimeInsert().execute().get();

It waits for the result of AsyncTask.

By doing this, will execute the AsyncTask first and then continues with the control flow.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Yatish
  • 521
  • 3
  • 14
0

If you fetch only one data than you can use this to solve your problem.. And use log to see if there is any err on fetching data... on catch block..Good luck.

private class InTimeInsert extends AsyncTask<Void, Void, Void> {

    String fetched_data = "";

    @Override
    protected Void doInBackground(Void... args) {

        try {
            arraylist = new ArrayList<HashMap<String, String>>();

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("at_username", uid));

            JSONObject json = jParser.makeHttpRequest(url_intime,"GET", params);

            //ownerObj = json.getJSONArray("visit");
            for (int i = 0; i < ownerObj.length(); i++) {
                jsonobject = ownerObj.getJSONObject(i);
                this.fetched_data = jsonobject.getString("at_itime");
            }
        } catch (Exception e) {
           Log.d("fetch err", e.toString());
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void args) {
        ina.setText(""+this.fetched_data);
    }
}
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/130670/discussion-on-answer-by-enamul-haque-how-to-reload-a-page-while-clicked-on-butto). – Bhargav Rao Dec 15 '16 at 07:42
  • OKEYY... now record is fetching... perfectly.. but the another problem arrives the button is not hideing... i set visibility for it in the if condition.. so when i click the button the data is displaying but Button remains as it is –  Dec 15 '16 at 07:56