如何使用Intent Extras传递Serializable对象的数组?

时间:2015-05-10 18:03:03

标签: android android-intent android-activity

我想在不使用首选项的情况下传递一个对象数组 我用Intent !!!

Object类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。

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活动:

    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[]

3 个答案:

答案 0 :(得分:1)

你可以使用一个包含Serializable数组的包装器类,我刚测试了它并且它可以工作。

首先,创建简单的包装类:

import java.io.Serializable;

public class BtsWrapper implements Serializable{
    Bts[] array;
    public BtsWrapper(Bts[] a){
        array = a;
    }
}

然后在Intent:

中传递Serializable包装类
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包装器,然后只提取数组:

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");
}

答案 1 :(得分:0)

我相信没有这样的putExtra API可以接受一些Serializable对象的Array。请参阅http://developer.android.com/reference/android/content/Intent.html#putCharSequenceArrayListExtra(java.lang.String,java.util.ArrayList)。还没有可用于从Intent中提取某些Serializable对象的数组的getter。

你可以让你的Bts类Parcelable并使用公共Intent putExtra(String name,Parcelable [] value)代替。

答案 2 :(得分:0)

创建Constants.java

public class Constants {
    public static String BTS_EXTRA_KEY = "bts_extra";
}

将您的Bts类修改为此类

import android.os.Parcel;
import android.os.Parcelable;

public class Bts implements Parcelable {

    private String nombts;
    private String ipaddress;
    private String logb;
    private String pawb;

    private int idbts;
    private int xbts;
    private int ybts;

    public Bts(Parcel in) {
        // Construct from parcel
        String[] data = new String[4];
        int[] dataInt = new int[3];

        in.readStringArray(data);
        in.readIntArray(dataInt);

        nombts = data[0];
        ipaddress = data[1];
        logb = data[2];
        pawb = data[3];

        idbts = dataInt[0];
        xbts = dataInt[0];
        ybts = dataInt[0];
    }


    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;
    }

    public String getNombts() {
        return nombts;
    }

    public void setNombts(String nombts) {
        this.nombts = nombts;
    }

    public String getIpaddress() {
        return ipaddress;
    }

    public void setIpaddress(String ipaddress) {
        this.ipaddress = ipaddress;
    }

    public String getLogb() {
        return logb;
    }

    public void setLogb(String logb) {
        this.logb = logb;
    }

    public String getPawb() {
        return pawb;
    }

    public void setPawb(String pawb) {
        this.pawb = pawb;
    }

    public int getIdbts() {
        return idbts;
    }

    public void setIdbts(int idbts) {
        this.idbts = idbts;
    }

    public int getXbts() {
        return xbts;
    }

    public void setXbts(int xbts) {
        this.xbts = xbts;
    }

    public int getYbts() {
        return ybts;
    }

    public void setYbts(int ybts) {
        this.ybts = ybts;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeStringArray(new String[]{nombts, ipaddress, logb, pawb});
        parcel.writeIntArray(new int[]{idbts, xbts, ybts});
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Bts createFromParcel(Parcel in) {
            return new Bts(in);
        }

        public Bts[] newArray(int size) {
            return new Bts[size];
        }
    };
}

现在要将此数据传递给另一个活动,您必须拥有ArrayList

中的数据
public void onBtnClick(View v){
    Intent i = new Intent(MainActivity.this, SecondActivity.class);
    ArrayList<Bts> listOfBts = new ArrayList<Bts>();
    listOfBts.add(new Bts(123, "323", "23423", "3423", "frlaldf", 45, 324));
    listOfBts.add(new Bts(124, "323", "23423", "3423", "frlaldf", 45, 324));
    listOfBts.add(new Bts(125, "323", "23423", "3423", "frlaldf", 45, 324));
    listOfBts.add(new Bts(126, "323", "23423", "3423", "frlaldf", 45, 324));
    listOfBts.add(new Bts(127, "323", "23423", "3423", "frlaldf", 45, 324));
    listOfBts.add(new Bts(128, "323", "23423", "3423", "frlaldf", 45, 324));
    i.putParcelableArrayListExtra(Constants.BTS_EXTRA_KEY, listOfBts);
    startActivity(i);
}

在其他活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    printBts();
}

private void printBts(){
    Bundle extras = getIntent().getExtras();
    ArrayList<Bts> btsList = extras.getParcelableArrayList(Constants.BTS_EXTRA_KEY);

    for(Bts b : btsList){
        Log.d("SecondActivity", ""  + b.getIdbts());
    }
}

结果:

05-10 21:14:51.414  19703-19703/si.kseneman.btstest D/SecondActivity﹕ 123
05-10 21:14:51.414  19703-19703/si.kseneman.btstest D/SecondActivity﹕ 124
05-10 21:14:51.414  19703-19703/si.kseneman.btstest D/SecondActivity﹕ 125
05-10 21:14:51.414  19703-19703/si.kseneman.btstest D/SecondActivity﹕ 126
05-10 21:14:51.414  19703-19703/si.kseneman.btstest D/SecondActivity﹕ 127
05-10 21:14:51.414  19703-19703/si.kseneman.btstest D/SecondActivity﹕ 128