我想以相同的格式将以下数组传递给我的receiver
类,并以相同的格式获取它,即数组应保持其结构。
curDts= new String[][] {{"1","2","3"}, {}, {"4","5","6"}};
以下是我到目前为止所尝试的内容,
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));
}
答案 0 :(得分:2)
String []是可序列化的对象以及String [] [],这意味着你只需在intent intent.putExtra(key,serializableObject)
中使用ready方法,在接收方你可以说intent.getSerializableExtra(KEY);
如果那就是你要找的东西。
<强>被修改强>
要传递数据,请执行以下操作:
Object[] objArr = new Object[]{ new String[]{"2"} ,new String[]{"5"}};
intent.putExtra("dates", objArr);
并挽回他们:
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]);
}
}
}
};
答案 1 :(得分:0)
执行此操作的一种方法是将较大数组中的每个String数组编码为String,并通过Intent传递String数组。该String数组可以由接收Intent的Activity解码。
我写了一个可以编码/解码String数组的类,可以在这里找到:https://gist.github.com/liangricha/10759438。
或者,您可以查看序列化。
答案 2 :(得分:0)
您可以传递整数,并且可以传递String[]
,所以:
int i = 0;
intent.putExtra("size", curDts.length);
for (String[] value : curDts) {
intent.putExtra("item"+ i++, value);
}
要检索它:
int size = intent.getIntExtra("size", 0);
String[][] curDts = new String[][size];
for (int i = 0; i < size; i++) {
curDts[i] = intent.getStringArrayExtras('item" + i);
}
答案 3 :(得分:0)
// 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));
}
}
}
}