如何在单击按钮时重新加载页面?

时间:2016-12-15 05:30:48

标签: android json reload

在我的应用程序中,我想从数据库中检索数据。但我面临的问题是,数据是从数据库中提取的,但是当我在显示数据时重新打开页面时,它不显示。我想在点击Button时重新加载页面。

Layout

这里的代码如下: -

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


    }
}   

3 个答案:

答案 0 :(得分:0)

//put your code in onResume methods
    @Override protected void onResume() {
        super.onResume();
       // call here
        new InTimeInsert().execute();
    }

答案 1 :(得分:0)

在调用.get();时添加AsyncTask

像:

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

它等待AsyncTask的结果。

通过执行此操作,将首先执行AsyncTask,然后继续控制流程。

答案 2 :(得分:0)

如果您只获取一个数据,则可以使用此数据来解决问题。 并使用日志查看是否有任何错误提取数据...在catch块上。祝你好运。

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