Pass Vector to subactivity

Put your problem here if it does not fit any of the other categories.

Pass Vector to subactivity

Postby zeeshan » Tue Mar 18, 2008 11:22 am

how can i Pass Vector to subactivity?? please help me out
zeeshan
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Mar 03, 2008 6:10 am

Top

Postby plusminus » Fri Mar 21, 2008 11:58 am

It is possible, using Parcelables (Search for that here on anddev).

But it is currently not working with Eclipse-only tools. :!:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby bluefloyd8 » Sun Mar 23, 2008 7:17 am

The had support for arrays in bundles in M3 but got rid of it in M5.

I used the following way: Create a putArray() and getArray() function which adds elements to the bundle with similar tags. Parcels might be a better solution. This one was simple enough for me to use though :)

Code: Select all
private void putStringArr(Bundle b, String[] arr, String name)
    {
       b.putInt(name+"_len", arr.length);
       for(int i=0; i<arr.length; i++){
          b.putString(name.concat(String.valueOf(i)), arr[i]);
       }
    }
    private String[] getStringArr(Bundle b, String name)
    {
       int length = b.getInt(name.concat("_len"));
       String[] res = new String[length];
       for(int i=0; i<length; i++)
       {
          res[i] = b.getString(name.concat(String.valueOf(i)));
       }    
       
       return res;
    }
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Postby Ressor » Sat Dec 05, 2009 5:14 am

Any chance this was ever fixed and there is some code example of passing a vector?

I've tried...

Putting the Vector "test" into the Bundle:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         if (nactbutton != null) {
  3.  
  4.           // Listen for the button being hit
  5.  
  6.           nactbutton.setOnClickListener(new OnClickListener() {
  7.  
  8.                public void onClick(View viewParam) {
  9.  
  10.                  
  11.  
  12.                    Intent myIntent = new Intent();
  13.  
  14.                    Bundle extras = new Bundle();
  15.  
  16.                    extras.putSerializable("mvector", test);
  17.  
  18.                    myIntent.putExtras(extras);
  19.  
  20.                    myIntent.setClassName("com.vectortest", "com.vectortest.vectorout");
  21.  
  22.                    //myIntent.putExtra("mvector", test);
  23.  
  24.                    //myIntent.putSerializable("mvector", test);
  25.  
  26.                    startActivity(myIntent);  
  27.  
  28.      
  29.  
  30.                }
  31.  
  32.           });
  33.  
  34.         }
  35.  
  36.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Trying to get it back out in the next Activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                test = (Vector<ParsedExampleDataSet>) getIntent().getSerializableExtra("mvector");              
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

Please help.
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Postby padde » Sat Dec 05, 2009 7:50 am

Two things you should check:

1. A vector gets converted to ArrayList.. so check in the receiving Activity if cast to ArrayList works.
2. If the elements of the vector are of your own class this class must implement serializable.

Here is an example that shows both cases:
One.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.ActivityExchangeTest;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Vector;
  5.  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9.  
  10. public class One extends Activity {
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.        
  15.         Vector<String> strings = new Vector<String>();
  16.         strings.add("string1");
  17.         strings.add("string2");
  18.         strings.add("string3");
  19.        
  20.         MyClass m1 = new MyClass(1,"a", strings);
  21.         MyClass m2 = new MyClass(2,"b", strings);
  22.        
  23.         Vector<MyClass> myclasses = new Vector<MyClass>();
  24.         myclasses.add(m1);
  25.         myclasses.add(m2);
  26.        
  27.         Intent i = new Intent();
  28.         i.putExtra("vec", strings);
  29.         i.putExtra("myc", myclasses);
  30.         i.setClassName("de.padde.ActivityExchangeTest", "de.padde.ActivityExchangeTest.Two");
  31.         startActivity(i);          
  32.     }
  33. }
  34.  
  35. class MyClass implements Serializable {
  36.     private static final long serialVersionUID = 1L;
  37.        
  38.     private int blub;
  39.     private String bla;
  40.     private Vector<String> strings;
  41.        
  42.     public MyClass(int a, String b, Vector<String> c) {
  43.         this.blub = a;
  44.         this.bla = b;
  45.         this.strings = c;
  46.     }
  47.  
  48.     public int getBlub() { return this.blub; }
  49.     public String getBla() { return this.bla; }
  50.     public Vector<String> getStrings() { return this.strings; }
  51. }
  52.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Two.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.padde.ActivityExchangeTest;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8.  
  9. public class Two extends Activity {
  10.     public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.main);
  13.        
  14.         ArrayList<String> strings = (ArrayList<String>) getIntent().getSerializableExtra("vec");
  15.        
  16.         ArrayList<MyClass> myclasses = (ArrayList<MyClass>) getIntent().getSerializableExtra("myc");
  17.        
  18.         Log.v("", strings.get(0));
  19.        
  20.         Log.v("", myclasses.get(0).getBla());
  21.         Log.v("", myclasses.get(0).getStrings().get(0));
  22.     }
  23. }
  24.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


res/layout/main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. </LinearLayout>
  8.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="de.padde.ActivityExchangeTest"
  4.    android:versionCode="1"
  5.    android:versionName="1.0">
  6.     <application
  7.        android:icon="@drawable/icon"
  8.        android:label="ActivityExchangeTest">
  9.         <activity android:name=".One" android:label="One">
  10.             <intent-filter>
  11.                 <action android:name="android.intent.action.MAIN"/>
  12.                 <category android:name="android.intent.category.LAUNCHER"/>
  13.             </intent-filter>
  14.         </activity>
  15.         <activity android:name=".Two" android:label="Two"/>
  16.     </application>
  17.     <uses-sdk android:minSdkVersion="4"/>
  18. </manifest>
  19.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Ressor » Sat Dec 05, 2009 2:52 pm

Wow! This is great! Thank you very much for this example.

I have one last question of you. While trying to figure out how to do this or something similar, I found that there is an android.app.Application class that can be used to store data/objects for easy access from other Activities.
It doesn't even require that I use a Bundle at all.

I tried the code below with the MyApplication class as a separate file and it worked, but I'm wondering now what is the best approach to this in your opinion.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.vectortest;
  3.  
  4.  
  5.  
  6. import java.util.Vector;
  7.  
  8.  
  9.  
  10. import android.app.Application;
  11.  
  12.  
  13.  
  14. public class MyApplication extends Application{
  15.  
  16.     private Vector<ParsedExampleDataSet> thing;
  17.  
  18.    
  19.  
  20.     public Vector<ParsedExampleDataSet> getThing(){
  21.  
  22.         return thing;
  23.  
  24.     }
  25.  
  26.  
  27.  
  28.     public void setThing( Vector<ParsedExampleDataSet> thing ){
  29.  
  30.         this.thing = thing;
  31.  
  32.     }
  33.  
  34. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


The sending Class (excerpt with "test" as the Vector):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. app = ((MyApplication)getApplication());
  3.  
  4.                 app.setThing(test);
  5.  
  6.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


The Reciver Class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.vectortest;
  3.  
  4.  
  5.  
  6. import java.util.Vector;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.widget.TextView;
  15.  
  16. import android.app.Application;
  17.  
  18.  
  19.  
  20. public class vectorout extends Activity {
  21.  
  22.         TextView tv;
  23.  
  24.         String text;
  25.  
  26.         private MyApplication app;
  27.  
  28.         Vector<ParsedExampleDataSet> test;
  29.  
  30.        
  31.  
  32.         public void onCreate(Bundle savedInstanceState) {
  33.  
  34.                super.onCreate(savedInstanceState);
  35.  
  36.                setContentView(R.layout.vectorout);
  37.  
  38.                
  39.  
  40.                
  41.  
  42.                tv = (TextView)findViewById(R.id.textview);
  43.  
  44.                text = "";
  45.  
  46.                
  47.  
  48.                 app = ((MyApplication)getApplication());
  49.  
  50.                 test = app.getThing();
  51.  
  52.                
  53.  
  54.                 for(int i=0; i < test.size(); i++)
  55.  
  56.                         text += test.get(i).toString()+"\n-------------------------\n";
  57.  
  58.          
  59.  
  60.            
  61.  
  62.                        
  63.  
  64.         tv.setText(text);
  65.  
  66.  
  67.  
  68.            }
  69.  
  70. }
  71.  
  72.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="com.vectortest.MyApplication">
  3.  
  4.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Top

Postby padde » Sat Dec 05, 2009 3:20 pm

You could descripe the application class as a convenient way to solve a special case of activity data exchange.
Its only for your own activities in your app.. if you need to send data between just those its perfect and you should
really prefer it over intent/bundle sending of your own.
But for almost any other case you will have to use the more generic way of sending bundles with intents.
There are more cases of convenient methods/classes for common generic problems.. i would always prefer the special convenient way if its applicable.. simply because its often alot easier to use and i think they use internally
optimized approaches.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Ressor » Sat Dec 05, 2009 5:13 pm

Thank you again for your help!
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests