操作栏菜单项单击时无法显示“进度”对话框

时间:2016-08-22 06:38:17

标签: android web-services android-asynctask fragment progressdialog

当我点击其中任何一个从webservice检索到的数据时,我在操作栏中有3个菜单项。但是在检索时我需要在屏幕上显示进度对话框。我尝试了不同的方法,比如在新线程AsyncTask中启动。但终于卡住了。请任何人帮助我。这是我的完整代码。

#!/usr/bin/env python
from pprint import pprint
import json
from twisted.web import server, resource
from twisted.internet import reactor
from JasminIntegration import *
import ast

class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        return "<html>Hello, world!</html>"

    def render_POST(self, request):
        pprint(request.__dict__)
        newdata = request.content.getvalue()
        newdata = ast.literal_eval(newdata)
        ret = runScenario(newdata)
        #print  request.content
        #print newdata
        return ''
site = server.Site(Simple())
reactor.listenTCP(7034, site)
reactor.run()

activity_line_chart.xml

    import hello.aws.graph.BatteryGraph;

public class BatteryGraph extends AppCompatActivity {

    static ProgressDialog dialog = null;
    static String value;
    public static Menu menu;

    static ArrayList<TrackingBean> Battery;
    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_line_chart);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent newIntent;


        switch (item.getItemId()) {



            case R.id.bb_menu_favorites:


                    value="favorites";
                    getData(value);

                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
                break;

            case R.id.bb_menu_mylist:

                    value="mylist";
                    getData(value);

                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();

                break;

            case R.id.bb_menu_recents:

                    value="myrecents";
                   getData(value);

                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();

                break;

        }
        return true;
    }
    public void getData(final String value)
    {

        AsyncCallWS task = new AsyncCallWS();
        try {
            task.execute().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    /**
     * A fragment containing a line chart.
     */
    public static class PlaceholderFragment extends Fragment {

        private LineChartView chart;
        private LineChartData data;


        public PlaceholderFragment() {


        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            setHasOptionsMenu(true);


            View rootView = inflater.inflate(R.layout.fragment_line_chart, container, false);

            chart = (LineChartView) rootView.findViewById(R.id.chart);



            generateData();


            return rootView;
        }


        private void generateData() {


        /****This method used to Plot points on graph ****/




    }
    }

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


        @Override
        protected void onPreExecute() {

            dialog = new ProgressDialog(BatteryGraph.this, R.style.MyTheme);

            dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);

            dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.cp_bar));

            dialog.setCancelable(false);
            dialog.show();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();



        }

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

            Battery=WebService.getDataFromWS(value);

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {

            if(dialog!=null||dialog.isShowing()) {
                dialog.dismiss();
            }

        }
    }



}

fragment_line_chart.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hello.aws.graph.BatteryGraph"
tools:ignore="MergeRootFrame" />

1 个答案:

答案 0 :(得分:0)

我希望它能帮到你...

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


    @Override
    protected void onPreExecute() {

pDialog = new ProgressDialog(getActivity());//here activity name or if u are using fragment put getActivity()
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.setCanceledOnTouchOutside(false);
        pDialog.show();
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();



    }

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

        Battery=WebService.getDataFromWS(value);

        return null;

    }

    @Override
    protected void onPostExecute(Void result) {

        if(dialog!=null||dialog.isShowing()) {
            pDialog.dismiss();
        }

    }
}