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

How to Reload a Page while clicked on Button?

在我的应用程序中,我想从数据库中检索数据。但是我面临的问题是,数据是从数据库中获取的,但是当我在显示数据时重新打开页面时它没有显示。我想在单击按钮时重新加载页面。

这里代码如下:-

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


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

添加 .get();,同时调用 AsyncTask

喜欢:

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

等待AsyncTask的结果。

通过这样做,将首先执行 AsyncTask 然后继续控制流程。

如果您只获取一个数据,那么您可以使用它来解决您的问题.. 并使用日志查看获取数据时是否有任何错误...在 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);
    }
}