多个意图后,getStringExtra返回null

时间:2017-01-02 19:26:57

标签: java android

我从getStringExtra接收一个值,然后我将此值发送到可以转到多个意图的开关。如果我发送一个意图,然后我返回到先前的意图,该值为空。

  

公共类Select_Dispo扩展了AppCompatActivity {

ListView listview;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select__dispo);
    listview = (ListView) findViewById(R.id.listView);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Intent intent = getIntent();
    final String value = intent.getStringExtra("divisao");

    RequestQueue queue = Volley.newRequestQueue(this);
    try {
        String url = "http://brunos.000webhostapp.com/teste/listar_dispositivos.php";
        JsonArrayRequest jsonRequest = new JsonArrayRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
                    public void onResponse(JSONArray response) {
                        try {
                            final ArrayList<String> dispositivos = new ArrayList<>();
                            ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),R.layout.custom_divi, dispositivos);
                            Integer i = 0;
                            String dispositivo;
                            while (i!= response.length()){
                                JSONObject obj = response.getJSONObject(i);
                                dispositivo = obj.getString("TABLE_NAME");
                                dispositivos.add(dispositivo);
                                i++;
                            }
                            listview.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });
        queue.add(jsonRequest);
    } catch (Exception ex) {
    } finally {
    }


    listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            switch( position )
            {
                case 0:
                    Intent alarme = new Intent(getApplicationContext(), AddAlarme.class);
                    String asd= (String) listview.getItemAtPosition(position);
                    alarme.putExtra("dispositivo", asd);
                    alarme.putExtra("divisao", value);
                    startActivity(alarme);
                    break;
                case 1:
                    Intent ar = new Intent(getApplicationContext(), AddArCondicionado.class);
                    String ar_con= (String) listview.getItemAtPosition(position);
                    ar.putExtra("dispositivo", ar_con);
                    ar.putExtra("divisao", value);
                    startActivity(ar);

                    break;
               case 2:
                   Intent audio = new Intent(getApplicationContext(), AddAudio.class);
                   String getAudio= (String) listview.getItemAtPosition(position);
                   audio.putExtra("dispositivo", getAudio);
                   audio.putExtra("divisao", value);
                   startActivity(audio);
                    break;
                case 3:
                    Intent estore = new Intent(getApplicationContext(), AddEstore.class);
                    String getEstore= (String) listview.getItemAtPosition(position);
                    estore.putExtra("dispositivo", getEstore);
                    estore.putExtra("divisao", value);
                    startActivity(estore);
                    break;
                case 4:
                    Intent iluminacao = new Intent(getApplicationContext(), AddIluminacao.class);
                    String getIlu= (String) listview.getItemAtPosition(position);
                    iluminacao.putExtra("dispositivo", getIlu);
                    iluminacao.putExtra("divisao", value);
                    startActivity(iluminacao);
                    break;
                case 5:
                    Intent porta = new Intent(getApplicationContext(), AddPorta.class);
                    String getPorta= (String) listview.getItemAtPosition(position);
                    porta.putExtra("dispositivo", getPorta);
                    porta.putExtra("divisao", value);
                    startActivity(porta);
                    break;
                case 6:
                    Intent comando = new Intent(getApplicationContext(), AddComando.class);
                    String getComando= (String) listview.getItemAtPosition(position);
                    comando.putExtra("dispositivo", getComando);
                    comando.putExtra("divisao", value);
                    startActivity(comando);

                    break;
            }
        }
    });

    Button btn = (Button)findViewById(R.id.button_instal_ok);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent x = new Intent(Select_Dispo.this, Ask_Dispo.class);
            startActivity(x);
        }
    });


}

}

1 个答案:

答案 0 :(得分:0)

您的活动和onCreate看起来应该是这样的,这样您就可以使用您的价值并保留它。

ListView listview;
String value;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // your code here
    value = intent.getStringExtra("divisao");
    // your code here
}