2

Estou criando um app para os alunos da minha escola, onde os alunos iram logar com sua matricula, as disciplinas serão colocadas em uma listview e quando o aluno clicar em um item da listview sera mostrado as notas em outra activity. OBS: as disciplinas serão preenchidas no listview por json.

protected Void doInBackground(Void... arg0) {
        HttpHandler httpHandler = new HttpHandler();

        // request to json data url and getting response
        String Jsonurl = "http://192.168.0.12/ProjetoNewWebService/Dados.php?codAlu="+code;
        String jsonString = httpHandler.makeServiceCall(Jsonurl);
        Log.e(TAG, "Response from url: " + jsonString);
        if (jsonString != null) {
            try {
                JSONObject jsonObject = new JSONObject(jsonString);
                // Getting JSON Array node
                JSONArray contacts = jsonObject.getJSONArray("dados");

                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);
                    Aluno = c.getString("nomeAlu");
                    String nomeDisc = c.getString("nomeDisc");
                    String nomeProf = c.getString("nomeProf");
                    String emailProf = c.getString("emailProf");
                    String nota1 = c.getString("nota1");
                    String nota2 = c.getString("nota2");

                    // Phone
                    //JSONObject phone = c.getJSONObject("phone");
                    //String mobile = phone.getString("mobile");
                    //String home = phone.getString("home");
                    //String office = phone.getString("office");

                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    // adding each child node to HashMap key => value
                    contact.put("nomeAlu", Aluno);
                    contact.put("name", nomeDisc);
                    contact.put("professor", nomeProf);
                    contact.put("email", emailProf);
                    contact.put("nota1", nota1);
                    contact.put("nota2", nota2);

                    // adding contact to contact list
                    contactJsonList.add(contact);

                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

        } else {
            Log.e(TAG, "Could not get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Could not get json from server.",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (progressDialog.isShowing())
            progressDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactJsonList,
                R.layout.list_item, new String[]{"name", "professor",
                "email","nota1","nota2"}, new int[]{R.id.name,
                R.id.professor, R.id.email, R.id.nota1, R.id.nota2});

        listView.setAdapter(adapter);
        txtAluno.setText(Aluno);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int i, long l) {




            }

        });
    }

1 Resposta 1

1

Faça sua classe aluno implementar a interface Parcelable.

Após, adicione após o onItemClick da listview:

startActivity(new Intent(getBaseContext(),Activity.class).putExtra("aluno",object));

Entao na Activity que voce quer recuperar o objeto, tu vai recupera-lo atráves de:

Intent it = getIntent(); Aluno aluno = it.getParcelableExtra("aluno");

Ou simplesmente Aluno aluno = getIntent().getParcelableExtra("aluno");

1
  • Você também pode usar a library GSON para serializar um objeto Aluno para uma String JSON, passar essa String como um Extra comum e depois na outra Activity recuperar o objeto desse Extra usando o GSON novamente. A vantagem é não precisar modificar a classe Aluno (transformar ela em Parcelable). Essa serialização/de-serialização com o GSON é feito em apenas 1 ou 2 linhas de código. 29/05/2017 às 19:49

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .