How can I take a specific item from a listview and place it in another Activity?

Asked

Viewed 283 times

1

I am creating an app for students at my school, where students will log in with their enrollment, the subjects will be placed in a listview and when the student clicks on an item from listview will be shown the notes in another activity. OBS: the disciplines will be filled in listview for 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 answer

0

Make your student class implement the interface Parcelable.

After, add after the onItemClick of listview:

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

So in Activity that Voce wants to recover the object, you will recover it through:

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

Or simply Aluno aluno = getIntent().getParcelableExtra("aluno");

  • You can also use the GSON library to serialize a Student object to a JSON String, pass this String as a common Extra, and then in the other Activity retrieve the Extra object using GSON again. The advantage is not having to modify the Student class (turn it into Parcelable). This serialization/de-serialization with GSON is done in only 1 or 2 lines of code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.