2

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);
Aditya Joshi
  • 69
  • 1
  • 2
  • 6

6 Answers6

1

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

Community
  • 1
  • 1
ErasmoOliveira
  • 1,416
  • 2
  • 20
  • 40
1

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");
Fahim
  • 12,198
  • 5
  • 39
  • 57
0

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[]).

curob
  • 705
  • 4
  • 17
0

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

eVolve
  • 1,340
  • 1
  • 11
  • 30
Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41
0

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

Ravichandra S V
  • 129
  • 1
  • 6
  • I've formatted your code for you - but to make this answer actually useful, please include some explanation on how it solves the OP's issue. – Tom May 25 '17 at 08:37
0

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!

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69