如何在另一个活动中执行一个类

时间:2017-04-07 11:31:25

标签: java android android-activity android-asynctask

我正在尝试从另一个活动(DetailsActivity)执行一个类(GetDirecoes,它位于MapsActivity中),我的想法是,当我点击浮动操作按钮(DetailsActivity)时,我执行该类(GetDirecoes)和然后关闭我当前的活动(DetailsActivity),当我关闭此活动时,它将自动打开我的父活动(MapsActivity),这是GetDirecoes方法所在的位置。 我正试图用这个执行方法:

new MapsActivity.GetDirecoes().execute(latRepr, lngRepr);

但它无法解决错误:

  

MapsActivity'不是封闭类

android studio为我提供的修正是让GetDirecoes保持静态,但我不能这样做,因为如果我这样做,整个课程就会中断,这里是GetDirecoes课程:

public class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MapsActivity.this);
            pDialog.setMessage("Aguarde...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            HttpHandler sh = new HttpHandler();
            txtDistanciaTotal = (TextView) findViewById(R.id.txtDistanciaTotal);
            txtDuracaoTotal = (TextView) findViewById(R.id.txtDuracaoTotal);
            instrucoes = (TextView) findViewById(R.id.Instrucoes);
            String url1 = "https://maps.googleapis.com/maps/api/directions/json?origin=";
            String url2 = "&destination=";
            String url3 = "&key=AIzaSyBh6tmMrC6QaHMJewFGvGc8xOjc0WMajxQ&language=pt";
            String latRepr = params[0];
            String lngRepr = params[1];
            final String jsonStr = sh.makeServiceCall(url1 + mylatitude + "," + mylongitude + url2 + latRepr + "," + lngRepr + url3);
            Log.e(TAG, "Link: " + url1 + mylatitude + "," + mylongitude + url2 + latRepr + "," + lngRepr + url3);

            Log.e(TAG, "Resposta do URL: " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject root = new JSONObject(jsonStr);
                    JSONArray routes = root.optJSONArray("routes");
                    if (routes != null) {
                        JSONObject singleRout = (JSONObject) routes.get(0);
                        JSONArray legs = (JSONArray) singleRout.get("legs");
                        if (legs != null) {
                            JSONObject singleLeg = (JSONObject) legs.get(0);
                            //Distancia total
                            JSONObject distanceT = (JSONObject) singleLeg.get("distance");
                            if (distanceT != null) {
                                distT = distanceT.getString("text");
                            }
                            //Duracao Total
                            JSONObject durationT = (JSONObject) singleLeg.get("duration");
                            if (durationT != null) {
                                duraT = durationT.getString("text");
                            }

                            JSONArray steps = (JSONArray) singleLeg.get("steps");
                            if (steps != null) {
                                for (int j = 0; j < steps.length(); j++) {
                                    JSONObject singleStep = (JSONObject) steps.get(j);
                                    HashMap<String, String> direcao = new HashMap<>();
                                    JSONObject distance = (JSONObject) singleStep.get("distance");
                                    //Distancia
                                    if (distance != null) {
                                        String dist = distance.getString("text");
                                        direcao.put("textDi", dist);
                                    }
                                    //Duraçao
                                    JSONObject duration = (JSONObject) singleStep.get("duration");
                                    if (duration != null) {
                                        String dura = duration.getString("text");
                                        direcao.put("textDu", dura);
                                    }
                                    //Polylines
                                    JSONObject singlePolyline = (JSONObject) singleStep.get("polyline");
                                    if (singlePolyline != null) {
                                        String points = singlePolyline.getString("points");
                                        Map<String, String> caminho = new HashMap<>();
                                        caminho.put("points", points);
                                        listaPolylines.add((HashMap<String, String>) caminho);
                                    }
                                    //Instruções
                                    String html = singleStep.getString("html_instructions");
                                    direcao.put("html_instructions2", html);
                                    listaDirecoes.add(direcao);
                                }
                            }
                        }
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "ERROR: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            } else {
                Log.e(TAG, "ERROR");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Verifique a sua ligação à internet", Toast.LENGTH_LONG).show();
                    }
                });
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            final ListView list = (ListView) (MapsActivity.this).findViewById(R.id.listaDirecoes);
            ListAdapter adapter = new SimpleAdapter(
                    (MapsActivity.this),
                    listaDirecoes,
                    R.layout.list_item_direcoes,
                    new String[]{"html_instructions2", "textDi", "textDu"},
                    new int[]{R.id.Instrucoes, R.id.txtDistancia, R.id.txtDuraçao});

            list.setAdapter(adapter);

            txtDuracaoTotal.setText(duraT);
            txtDistanciaTotal.setText(distT);

            fazerCaminho(listaPolylines);


            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }

            bottom_sheet.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    Log.v(TAG, "Parent Touch");
                    findViewById(R.id.listaDirecoes).getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            });
            listaDirecoesChild.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    Log.v(TAG, "CHILD TOUCH");
                    // Disallow the touch request for parent scroll on touch of child view
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以浏览Call a Class From another class

((DetailsActivity) activity).refreshList(latRepr, lngRepr);

您应该初始化,活动

从另一个班级,将其命名为:

public void refreshList(String latRepr, String lngRepr) {
      new GetDirecoes().execute(latRepr, lngRepr);
   }

现在,您可以使用DetailsActivity.class,

@font-face