1

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();
        }
3
  • Add your values into ArrayList<String>, Pass to another activity using Intent, retrieve List of Strings into another activity and bind the values into ListView Commented Aug 28, 2017 at 5:49
  • Even doing it the way you have done is also fine. What is the issue you are facing? Commented Aug 28, 2017 at 5:49
  • I finally displayed the arraylist values to the listview!! Thank you guys Commented Aug 28, 2017 at 8:30

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is this the right way of passing my arraylist through intent? Here is my updated code: pastebin.com/Gu88pBtW
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.