Using Intent for array

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

Using Intent for array

Postby carlovep » Mon Sep 02, 2013 8:33 am

Hi. I'm new for android developer. I want to ask about intent array.. I wany to send an array into different package.
First of all I have class activityToSmartWatch and I have rectangular array called storePrintIMC. It received an array from another class in one package.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.PrinterStatus.AppLab;
  2.  
  3.  
  4. import java.util.ArrayList;
  5.  
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.TextView;
  14.  
  15. import com.example.printerstatus.R;
  16. import com.sonyericsson.extras.liveware.extension.controlsample.ControlWatch;
  17.  
  18. public class ActivityToSmartWatch extends Activity {
  19.  
  20.         private Activity activity;
  21.         private ArrayList<String[]> data;
  22.         private static LayoutInflater inflater = null;
  23.         public String[] inkLevels = new String[5];
  24.         public final String EXTRA_MESSAGE = "com.PrinterStatus.Lab.MESSAGE";
  25.         View message;
  26.         String [][] storePrintIMC= new String [4][4];
  27.         public static final String ARRAYS_COUNT = "com.PrinterStatus.Lab.ARRAYS_COUNT";
  28.         public static final String ARRAY_INDEX = "com.PrinterStatus.Lab.ARRAY_INDEX";
  29.        
  30.          protected void onCreate(Bundle savedInstanceState) {
  31.                         super.onCreate(savedInstanceState);
  32.                
  33.         Bundle bundle = new Bundle();
  34.                 final String data[][] = storePrintIMC;
  35.                 int count = data.length;
  36.                 bundle.putInt(ARRAYS_COUNT, count);
  37.                 for (int i = 0; i < count; i++)
  38.                 {
  39.                         bundle.putStringArray(ARRAY_INDEX + i, data[i]);
  40.                 }
  41.                  Intent intent = new Intent(ActivityToSmartWatch.this, ControlWatch.class);
  42.          intent.putExtras(bundle);
  43.  
  44.          startActivity(intent);
  45.        
  46.         }      
  47.  
  48.        
  49.         public ActivityToSmartWatch(Activity a, ArrayList<String[]> inkLevels) {
  50.                 activity = a;
  51.                 data = inkLevels;
  52.                 inflater = (LayoutInflater) activity
  53.                                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  54.         }
  55.        
  56.  
  57.         public int getCount() {
  58.                 return data.size();
  59.         }
  60.  
  61.         public Object getItem(int position) {
  62.                 return position;
  63.         }
  64.  
  65.         public long getItemId(int position) {
  66.                 return position;
  67.         }
  68.  
  69.        
  70.         public String[] getPrintIMC (int position)
  71.         {
  72.                
  73.                 System.arraycopy(data.get(position), 0, inkLevels, 0, data.get(position).length);
  74.                
  75.  
  76.                 data.get(position);
  77.                 //sendMessage(message);
  78.                 if (position == 0 )
  79.                 {
  80.                        
  81.                         storePrintIMC[0][0]=inkLevels[0];
  82.                         storePrintIMC[0][1]=inkLevels[1];
  83.                         storePrintIMC[0][2]=inkLevels[2];
  84.                         storePrintIMC[0][3]=inkLevels[3];
  85.                 }
  86.                 if (position == 1 )
  87.                 {
  88.                         storePrintIMC[1][0]=inkLevels[0];
  89.                         storePrintIMC[1][1]=inkLevels[1];
  90.                         storePrintIMC[1][2]=inkLevels[2];
  91.                         storePrintIMC[1][3]=inkLevels[3];
  92.                 }
  93.                 if (position == 2 )
  94.                 {
  95.                         storePrintIMC[2][0]=inkLevels[0];
  96.                         storePrintIMC[2][1]=inkLevels[1];
  97.                         storePrintIMC[2][2]=inkLevels[2];
  98.                         storePrintIMC[2][3]=inkLevels[3];
  99.                 }
  100.                 if (position == 3 )
  101.                 {      
  102.                         storePrintIMC[3][0]=inkLevels[0];
  103.                         storePrintIMC[3][1]=inkLevels[1];
  104.                         storePrintIMC[3][2]=inkLevels[2];
  105.                         storePrintIMC[3][3]=inkLevels[3];
  106.                 }
  107.                 return inkLevels;
  108.                
  109.         }
  110.        
  111.        
  112.        
  113. }
  114.        
  115.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


After that I'm using an intent to send the array (I saw from view-layout-resource-problems-f27/how-can-i-pass-multidimensional-string-array-two-activities-t9259.html), in ActivityToSmartWatch I used:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    Bundle bundle = new Bundle();
  2.                 final String data[][] = storePrintIMC;
  3.                 int count = data.length;
  4.                 bundle.putInt(ARRAYS_COUNT, count);
  5.                 for (int i = 0; i < count; i++)
  6.                 {
  7.                         bundle.putStringArray(ARRAY_INDEX + i, data[i]);
  8.                 }
  9.                  Intent intent = new Intent(ActivityToSmartWatch.this, ControlWatch.class);
  10.          intent.putExtras(bundle);
  11.  
  12.          startActivity(intent);
  13.        
  14.         }      
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



In other package. I have made class that will received the inten. I have put ControlWatch.
Syntax: [ Download ] [ Hide ]
  1. package com.sonyericsson.extras.liveware.extension.controlsample; 
  2.  
  3. import java.util.ArrayList; 
  4.  
  5. import com.example.printerstatus.R; 
  6.  
  7. import android.app.Activity; 
  8. import android.os.Bundle; 
  9.  
  10. public class ControlWatch extends Activity{ 
  11. public String[] inkLevels = new String[5]; 
  12.      
  13.      @Override 
  14.       public void onCreate(Bundle savedInstanceState) { 
  15.         super.onCreate(savedInstanceState); 
  16.         Bundle bundle = getIntent().getExtras(); 
  17.         if (bundle != null) { 
  18.  
  19.             int count = bundle.getInt(ActivityToSmartWatch.ARRAYS_COUNT, 0); 
  20.  
  21.             ArrayList<String[]> arrays = new ArrayList<String[]>(count); 
  22.  
  23.             for (int i = 0; i < count; i++) 
  24.  
  25.                 arrays.add(bundle.getStringArray(ActivtyToSmartWatch.ARRAY_INDEX + i)); 
  26.  
  27.             String[][] data = arrays.toArray(new String[][]{}); 
  28.  
  29.         } 
  30.      } 
  31.  
  32. }  


I can't run the application because I have an errors.
Inside the activityToSmartWatch, I have got error in
Code: Select all
Intent intent = new Intent(ActivityToSmartWatch.this, ControlWatch.class);

it said : ControlWatch cannot be resolved to a type

For the ControlWatch class, I have got error in
Code: Select all
int count = bundle.getInt(ActivityToSmartWatch.ARRAYS_COUNT, 0);

It said:ActivityToSmartWatch cannot be resolved to a variable

Do you know how to solve it?
Thanks
carlovep
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Sep 02, 2013 8:13 am

Top

Re: Using Intent for array

Postby chicksen22 » Mon Sep 02, 2013 9:59 am

This was actually what I was in search of, and I’m glad to came right here!
chicksen22
Freshman
Freshman
 
Posts: 5
Joined: Mon Sep 02, 2013 9:54 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests