Aira Veronica Dizon
Aira Veronica Dizon

Reputation: 35

Intent values (json array) to Listview

Is it possible to put Intent values from one Activity to a ListView in another Activity?

Here are the values from Activity1 that I wanted to put to the ListView in Activity2:

       try
        {
        jsonobject = new JSONObject(json);
        jsonarray = jsonobject.getJSONArray("user");
        JSONObject jb= jsonarray.getJSONObject(0);
        //Username = jb.getString("Username");
        Password = jb.getString("Password");
        Fullname = jb.getString("Fullname");
        Email = jb.getString("Email");
        Bio = jb.getString("Bio");
        Location = jb.getString("Location");


        fn.setText(Fullname);
        em.setText(Email);
        loc.setText(Location);
        b.setText(Bio);

        if(json!=null)
        {
            Intent i = new 
  Intent(HomePageActivity.this,ProfileActivity.class);
            i.putExtra(u.username(), u.getUsername());
            i.putExtra("password",Password);
            i.putExtra("fullname",Fullname);
            i.putExtra("email", Email);
            i.putExtra("bio", Bio);
            i.putExtra("location", Location);
            startActivity(i);
            finish();
        }



        }catch(Exception e)
        {
            e.printStackTrace();
        }

Upvotes: 1

Views: 180

Answers (2)

Sachin Aggarwal
Sachin Aggarwal

Reputation: 1135

Yes, you can. I can see that you want to pass all the values of your user object to another activity using intent. You can pass the individual values like you are doing above OR You can pass the user object directly as -

i.putExtra("MyClass", u); //u is the user object here

You can get back your user object in the target activity as -

getIntent().getSerializableExtra("MyClass"); //Make sure that your user model class implements serializable

If it is a listview of users, you can add this user object to your array of users. This is the array that you must have set to your adapter in target activity. Now you can call -

adapter.notifyDataSetChanged();

This will make the new changes in your listview. Thanks.

Upvotes: 0

Kyaw Kyaw
Kyaw Kyaw

Reputation: 37

You can put intent values from one activity to a listview in another activity.

From another activity, you can use getIntent().getExtras().getString("key...");. But this can be only one line.

For sending array, you should see Sending arrays with Intent.putExtra.

For sending arraylist, Passing ArrayList through Intent.

I hope this may help you.

Upvotes: 2

Related Questions