Aditya Joshi
Aditya Joshi

Reputation: 69

how to pass list of objects through intent

I am trying to pass the list of object from one activity to another. But not able to send it.

Here is my code.

 setPrefBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent homePageIntent = new Intent(SetPreferences.this,
                        KlarityHome.class);// Connecting setPreferences page
                // with Klarity Home Page

               List<PracticeVO> tempList = new ArrayList<PracticeVO>();
               tempList.add(practiceObj);
                     homePageIntent.putExtra("SelectedPractice",       tempList.toArray());
                startActivity(homePageIntent);
            }
            });

And retriving in 2nd activity like this : Intent prefIntent = new Intent(); List preferencedPractices = (List) getIntent().getExtras().get("SelectedPractice");

            ArrayAdapter<PracticeVO> praticeAdapter = new ArrayAdapter<PracticeVO>(this, android.R.layout.simple_spinner_item,
                    preferencedPractices);

              praticeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            practiceSpin.setAdapter(praticeAdapter);

Upvotes: 2

Views: 9195

Answers (6)

Taslim Oseni
Taslim Oseni

Reputation: 6263

I was able to achieve it this way:

  • Make your model class (PracticeVO) as well as your Activity class (KlarityHome) implement Serializable.

    public class MyClass implements Serializable {...}
    
  • In your activity class, store your objects in an ArrayList rather than a List.

  • Finally, you can do this:

    // create intent
    Intent homePageIntent = new Intent(SetPreferences.this, KlarityHome.class);
    
    // create your ArrayList and add your item(s)
    List<PracticeVO> tempList = new ArrayList<PracticeVO>();
    tempList.add(practiceObj);
    
    // add your arraylist to your intent
    homePageIntent.putExtra("SelectedPractice", tempList);
    
    // go go go ;-)
    startActivity(homePageIntent);
    

I hope this helps. Merry coding!

Upvotes: 0

Ando Masahashi
Ando Masahashi

Reputation: 3122

implement Serializable in your PracticeVO class file

When sending from an activity:

List<PracticeVO> PracticeVO = new ArrayList<PracticeVO>;
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("PracticeVO", ArrayList<PracticeVO> PracticeVO);
startactivity(PracticeVO);

At the receiver side:

List<Question> questions = new ArrayList<Question>();
questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");

source: Link

Upvotes: 0

Ravichandra S V
Ravichandra S V

Reputation: 149

Notification fundinProgram = (Notification) getIntent().getSerializableExtra(DETAIL_OBJECT);

Upvotes: 0

Fahim
Fahim

Reputation: 12358

Try in this manner

ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);

In the Transfer.class

Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");

Upvotes: 1

ErasmoOliveira
ErasmoOliveira

Reputation: 1426

You need to use putExtra() function to pass values to another activity

Intent newIntent = new Intent(MainActivity.this, ResultActivity.class);
newIntent.putExtra("yourTxtField",yourTxtField.getText());

to receive you need to use other getIntent and Bundle.

Intent myIntent = getIntent();
Bundle valores  = myIntent.getExtras();

//Objeto que vai calcular os resultados
Resultado resultado = new Resultado();

//Recupera os valores recebidos pelo intent
double seuDinheiro  =  Double.parseDouble((String) valores.get("seuDinheiro").toString());

To pass objects see this SO post

How do I pass an object from one activity to another on Android?

How to pass an object from one activity to another on Android

Upvotes: 1

curob
curob

Reputation: 745

What type is PracticeVO? In order to do what you are intending, PracticeVO needs to implement the Parcelable interface (http://developer.android.com/reference/android/os/Parcelable.html). Then you can use this version of the putExtra() method: http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Parcelable[]).

Upvotes: 0

Related Questions