如何使用Intent Extras传递可序列化对象数组? [英] How to use Intent Extras to pass Array of Serializable objects?
本文介绍了如何使用Intent Extras传递可序列化对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想不使用首选项就传递对象数组 我用Intent !!!
I want to pass an array of objects without using the preference I use Intent!!!
对象类Bts
public class Bts implements Serializable {
int idbts;
String nombts;
String ipaddress;
String logb;
String pawb;
int xbts;
int ybts;
public Bts(int idbts, String nombts, String ipaddress, String logb,
String pawb, int xbts, int ybts) {
super();
this.idbts = idbts;
this.nombts = nombts;
this.ipaddress = ipaddress;
this.logb = logb;
this.pawb = pawb;
this.xbts = xbts;
this.ybts = ybts;
}
//wthis setter and getter
}
源活动 Ps:JSONParseBts填充了listeBts.
The source activity Ps:JSONParseBts filled listeBts.
public class ApresConnextionActivity extends Activity {
public Bts[] listeBts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.apresconnect);
final Button btsButton = (Button) findViewById(R.id.btbts);
//boutonbts
btsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.w("myApp",""+listeBts.length);
Intent i = new Intent(ApresConnextionActivity.this, BtsBoutonActivity.class);
i.putExtra("tabbts",listeBts);
startActivity(i);
}
});
new JSONParseBts().execute();
}
public class JSONParseBts extends AsyncTask<String, String, JSONObject> { ... }
}
与占卜活动:
public class BtsBoutonActivity extends Activity {
cellule[] listecellule;
Bts[] listeBts;
int i,xx=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.btsbouton);
Bundle b = getIntent().getExtras();
if (b != null)
{
Bts[] listeBts = (Bts[])b.getSerializable("tabbts") ;
Log.w("myApp",""+listeBts.length);
}else{
Log.w("myApp","you have problem");
}
/* Intent intent = getIntent();
listeBts = (Bts[])intent.getSerializableExtra("tabbts");*/
final Button[] b = new Button[listeBts.length];
LinearLayout ll3 = (LinearLayout)findViewById(R.id.linearLayout2); // Btn
for(i = 0; i < listeBts.length; i++){
//Log.w("myApp",listeBts[i].toString());
b[i] = new Button(BtsBoutonActivity.this);
b[i].setText(listeBts[i].getNombts());
xx =listeBts[i].getIdbts();
Log.w("myApp",""+xx);
b[i].setId(xx);
ll3.addView(b[i]);
final Button btbts = (Button) findViewById(xx);
btbts.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast saved_message=Toast.makeText(BtsBoutonActivity.this,btbts.getText()+" "+btbts.getId(),1);
saved_message.show();
}});
}
}
}
错误是:
05-10 16:29:25.096: E/AndroidRuntime(819): java.lang.RuntimeException: Unable to start activity ComponentInfo{pfe.essat.com/pfe.essat.com.BtsBoutonActivity}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to pfe.essat.objet.com.Bts[]
推荐答案
您可以使用包含Serializable数组的包装器类,我刚刚对此进行了测试,并且它可以工作.
You can use a wrapper class that contains the Serializable array, I just tested this and it works.
首先,创建您的简单包装器类:
First, create your simple wrapper class:
import java.io.Serializable;
public class BtsWrapper implements Serializable{
Bts[] array;
public BtsWrapper(Bts[] a){
array = a;
}
}
然后在Intent中传递Serializable包装器类:
Then pass the Serializable wrapper class in the Intent:
btsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.w("myApp",""+listeBts.length);
Intent i = new Intent(ApresConnextionActivity.this, BtsBoutonActivity.class);
BtsWrapper bWrapper = new BtsWrapper(listeBts); //added
i.putExtra("tabbts",bWrapper); //modified
startActivity(i);
}
});
然后,从附加项中获取Serializable包装器,然后提取数组:
Then, get the Serializable wrapper from the extras, and just extract the array:
Bundle b = getIntent().getExtras();
if (b != null)
{
//Bts[] listeBts = (Bts[])b.getSerializable("tabbts") ;
BtsWrapper bWrapper = (BtsWrapper) b.getSerializable("tabbts"); //added
Bts[] listeBts = bWrapper.array; //added
Log.w("myApp",""+listeBts.length);
}else{
Log.w("myApp","you have problem");
}
这篇关于如何使用Intent Extras传递可序列化对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文