I want to pass an array of objects without using the preference I use Intent!!!
The Object class 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
}
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> { ... }
}
And distination Activity:
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();
}});
}
}
}
The error is:
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[]
Create Constants.java
Fix your Bts class to be like this
Now to pass this data to another activity, you must have the data in an ArrayList
And in the other activity
Result:
You can use a wrapper class that contains the Serializable array, I just tested this and it works.
First, create your simple wrapper class:
Then pass the Serializable wrapper class in the Intent:
Then, get the Serializable wrapper from the extras, and just extract the array:
I believe there is no such putExtra API existent that accepts Array of some Serializable objects. See http://developer.android.com/reference/android/content/Intent.html#putCharSequenceArrayListExtra(java.lang.String, java.util.ArrayList) onwards. There is also no getter available for extracting Array of some Serializable objects from Intent.
You could perhaps make your Bts class Parcelable and use public Intent putExtra (String name, Parcelable[] value) instead.