Reputation: 419
I want to pass the following array to my receiver
class in the same format and fetch it in same format i.e the array should maintain its structure.
curDts= new String[][] {{"1","2","3"}, {}, {"4","5","6"}};
Following is what I have tried so far,
Intent intent = new Intent(this, AlarmReceiver.class);
for(int i = 0; i<curDts.length ; i++){
for(int j = 0; j<curDts[i].length; j++){
intent.putExtra("date"+i,"'"+curDts[i][j]+"',");
Log.v("sending","'"+curDts[i][j]+"',");
}
}
for(int i=0; i<12; i++){
Log.v("",""+arg1.getExtras().getString("date"+i));
}
Upvotes: 2
Views: 2096
Reputation: 21
You Can simply do like this: in the first class:
String[][] bidderList;
Intent intent = new Intent(class1.this, class2.class);
intent.putExtra("bidderList",bidderList);
startActivity(intent);
finish();
Then in the second class where you want to receive the 2d array do like this:
String[][] bidderList;
bidderList =(String[][]) getIntent().getSerializableExtra("bidderList");
And that is all :>, you will need to use for loop to access the String[][] array elements
Upvotes: 1
Reputation: 2705
String[] are serializable objects as well as String[][] that means you can simply use the ready method in the intent intent.putExtra(key,serializableObject)
, and on your receiver side you could say intent.getSerializableExtra(KEY);
if that what you was looking for.
Edited
To pass your data do something like :
Object[] objArr = new Object[]{ new String[]{"2"} ,new String[]{"5"}};
intent.putExtra("dates", objArr);
and to retreive them :
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Object[] obj = (Object[]) intent.getSerializableExtra("dates");
if(obj != null){
for (int i = 0; i < obj.length; i++) {
String[] object = (String[])obj[i];
Log.d(getClass().getSimpleName(), "OBJ : " + object[0]);
}
}
}
};
Upvotes: 2
Reputation: 24848
// try this way,hope this will help you...
Note : try to use ArrayList<ArrayList<String>> insted of two dimensional which can be easy Serializable.
**FirstActivty**
public class MyActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
ArrayList<String> first = new ArrayList<String>();
first.add("1");
first.add("2");
first.add("3");
ArrayList<String> second = new ArrayList<String>();
second.add("4");
second.add("5");
second.add("6");
ArrayList<String> third = new ArrayList<String>();
third.add("7");
third.add("8");
third.add("9");
list.add(first);
list.add(second);
list.add(third);
Intent intent =new Intent(this,MyActivity2.class);
intent.putExtra("SerializableList",list);
startActivity(intent);
}
}
**SecondActivity**
public class MyActivity2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
ArrayList<ArrayList<String>> list = (ArrayList<ArrayList<String>>) getIntent().getSerializableExtra("SerializableList");
for (int i=0;i<list.size();i++){
ArrayList<String> row = list.get(i);
for (int j=0;j<row.size();j++){
System.out.println("ParentIndex >> ChildIndex >> Value "+i+" >> "+j+" >> "+row.get(j));
}
}
}
}
Upvotes: 0
Reputation: 39397
You can pass integers, and you can pass String[]
, so:
int i = 0;
intent.putExtra("size", curDts.length);
for (String[] value : curDts) {
intent.putExtra("item"+ i++, value);
}
To retrieve it:
int size = intent.getIntExtra("size", 0);
String[][] curDts = new String[][size];
for (int i = 0; i < size; i++) {
curDts[i] = intent.getStringArrayExtras('item" + i);
}
Upvotes: 0
Reputation: 418
One way to do this is to encode each String array within the larger array of arrays into a String, and pass a String array through the Intent. This String array can be decoded by the Activity receiving the Intent.
I wrote a class that can encode/decode a String array, which can be found here: https://gist.github.com/liangricha/10759438.
Alternatively, you can look into Serialization.
Upvotes: 0