How can i pass multidimensional String array two activities

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How can i pass multidimensional String array two activities

Postby poch » Sun Nov 29, 2009 6:16 pm

I want to pass multi dimensional String array between two activities or pages.

I already tried with bundle.putextra like methods. But those allow only one dimensional string array.

I really hope you can help me on this .Please do reply.Its very urgent for my project .


Thank you!
poch
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Nov 29, 2009 5:55 pm
Location: INDIA

Top

Postby appforce » Sun Nov 29, 2009 8:59 pm

Hi,

You should implement it in your own way. Pass the count of arrays as a key, and append the index of each one to a corresponding key.

AppForce
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

About passing Multi dimensional array

Postby poch » Mon Nov 30, 2009 5:51 am

Hi Appforce,

Thanks for your reply.Could you please give me an example java code for that task.Here is my String array, final String[][] arr2={{"1","pavan"},{"2","kumar"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
I hope you do reply.

Thank you.

Regards
poch
poch
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Nov 29, 2009 5:55 pm
Location: INDIA

Postby appforce » Mon Nov 30, 2009 6:48 am

Hi,

This is how to store it to the bundle:

Bundle bundle = new Bundle();
final String ARRAYS_COUNT = "com.yourname.ARRAYS_COUNT";
final String ARRAY_INDEX = "com.yourname.ARRAY_INDEX";
final String data[][] = new String[][] {{"1","pavan"},{"2","kumar"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
int count = data.length;
bundle.putInt(ARRAYS_COUNT, count);
for (int i = 0; i < count; i++) {
bundle.putStringArray(ARRAY_INDEX + i, data[i]);
}
intent.putExtras(bundle);

Reading is opposite.

AppForce
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

About passing multi dimensional string array b/w activities

Postby poch » Mon Nov 30, 2009 1:03 pm

Thanks for your reply. Still i got problem with my code.Can you send some example code for reading also.Could you please give me proper solution for my code.

Here is my code :

Please go through the attachment you will find my code.
Attachments
second.java
(1.19 KiB) Downloaded 266 times
home.java
(1.63 KiB) Downloaded 240 times
poch
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Nov 29, 2009 5:55 pm
Location: INDIA

Postby appforce » Mon Nov 30, 2009 4:00 pm

Hi,

Try something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public class Home extends Activity {
  3.  
  4.                
  5.  
  6.                 public static final String ARRAYS_COUNT = "com.yourname.ARRAYS_COUNT";
  7.  
  8.                 public static final String ARRAY_INDEX = "com.yourname.ARRAY_INDEX";
  9.  
  10.                
  11.  
  12.                 protected void onCreate(Bundle savedInstanceState) {
  13.  
  14.                         super.onCreate(savedInstanceState);
  15.  
  16.                        
  17.  
  18.                         final String data[][] = new String[][] {{"1","pavan"},{"2","kumar"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
  19.  
  20.                         Bundle bundle = new Bundle();
  21.  
  22.                         int count = data.length;
  23.  
  24.                         bundle.putInt(ARRAYS_COUNT, count);
  25.  
  26.                         for (int i = 0; i < count; i++)
  27.  
  28.                                 bundle.putStringArray(ARRAY_INDEX + i, data[i]);
  29.  
  30.                         Intent intent = new Intent(this, Second.class);
  31.  
  32.                         intent.putExtras(bundle);
  33.  
  34.                         startActivity(intent);
  35.  
  36.                          
  37.  
  38.                 }
  39.  
  40.                
  41.  
  42.         }
  43.  
  44.        
  45.  
  46.         public class Second extends Activity {
  47.  
  48.                
  49.  
  50.                 protected void onCreate(Bundle savedInstanceState) {
  51.  
  52.                         super.onCreate(savedInstanceState);
  53.  
  54.                        
  55.  
  56.                         Bundle bundle = getIntent().getExtras();
  57.  
  58.                        
  59.  
  60.                         if (bundle != null) {
  61.  
  62.                                 int count = bundle.getInt(Home.ARRAYS_COUNT, 0);
  63.  
  64.                                 ArrayList<String[]> arrays = new ArrayList<String[]>(count);
  65.  
  66.                                 for (int i = 0; i < count; i++)
  67.  
  68.                                         arrays.add(bundle.getStringArray(Home.ARRAY_INDEX + i));
  69.  
  70.                                 String[][] data = arrays.toArray(new String[][]{});
  71.  
  72.                         }
  73.  
  74.                 }
  75.  
  76.                
  77.  
  78.         }
  79.  
  80.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



AppForce
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Top

Postby poch » Tue Dec 01, 2009 4:58 am

Thank you veryyyyyyyyyyy much. i got it.
poch
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Nov 29, 2009 5:55 pm
Location: INDIA

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests