在android中将对象的ArrayList从一个活动传递到另一个活动

时间:2013-03-06 09:02:25

标签: android

所以我希望借助ArrayList对象将数据从一个活动发送到另一个活动。下面是我想用作对象的类代码。所以请告诉我我哪里出错了。

class UserData implements Parcelable{
private String name, phone, city, username, password;
private String userDetails[];
private UserData(Parcel in){
    city = in.readString();
    name = in.readString();
    password = in.readString();
    phone = in.readString();
    username = in.readString();
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
    // TODO Auto-generated method stub
    out.writeStringArray(getDetails());
}

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

    @Override
    public UserData[] newArray(int size) {
        // TODO Auto-generated method stub
        return new UserData[size];
    }
};
/*set method to set all the data that will be taken from the edit texts*/
public UserData(String n, long p, String c, String user, String pass){
    name = n;
    phone = Long.valueOf(p).toString();
    city = c;
    username = user;
    password = pass;
}

/*get method to retrieve data in others activities such as signin and detailspage*/
String[] getDetails(){
    userDetails = new String[]{name, phone, city, username, password};
    return userDetails;
}

}

3 个答案:

答案 0 :(得分:0)

试试这个,使用putStringArrayListExtra

给你使用arraylist
Intent i = new Intent(MainActivity.this,Nextactivity.class);
i.putStringArrayListExtra("arraylist", arraylist);

得到arraylist。

Intent i = getIntent();
list = i.getIntegerArrayListExtra("arraylist");

答案 1 :(得分:0)

第1次活动

        Arraylist<String> values;

        Intent it=new Intent(MainActivity.this,Next.class);
        it.putExtra("value", values);
        startActivity(it);

第二项活动

  Arraylist<String> list;

list=(ArrayList<String>) getIntent().getSerializableExtra("value");

答案 2 :(得分:0)

首先在自定义类中创建两个方法

public class Qabir {

public int age;
public String name;

Qabir(){
}

Qabir(int age,String name){
    this.age=age; this.name=name;
}   

// method for sending object
public String toJSON(){
    return "{age:" + age + ",name:\"" +name +"\"}";
}

// method for get back original object
public void initilizeWithJSONString(String jsonString){

    JSONObject json;        
    try {
        json =new JSONObject(jsonString );
        age=json.getInt("age");
        name=json.getString("name");
    } catch (JSONException e) {
        e.printStackTrace();
    } 
}

}

然后创建一个发送对象列表的函数

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Qabir q1 = new Qabir(22, "KQ");
    Qabir q2 = new Qabir(23, "K Q");
    Qabir q3 = new Qabir(24, "K_Q");

    ArrayList<Qabir> list = new ArrayList<Qabir>();

    list.add(q1);
    list.add(q2);
    list.add(q3);

    Intent in = new Intent(this, SubActivity.class);
    in.putExtra("obj", arrayListToJSON(list));
    startActivity(in);
}

private String arrayListToJSON(ArrayList<Qabir> al) {
    JSONArray array = new JSONArray();
    try {
        for (int i = 0; i < al.size(); i++) {
            array.put(new JSONObject(al.get(i).toJSON()));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return array.toString();
}

并在接收器上创建另一个函数

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<Qabir> list = new ArrayList<Qabir>();

    list = getObjectList(getIntent().getStringExtra("obj"));

    Log.e("Activity 2", "" +list.size());
}

private ArrayList<Qabir> getObjectList(String st) {

    ArrayList<Qabir> list = new ArrayList<Qabir>();
    JSONArray array = null;
    try {
        array = new JSONArray(st);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (int i = 0; i < array.length(); i++) {
        try {
            Qabir q= new Qabir();
             q.initilizeWithJSONString(""+array.get(i));
             list.add(q);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return list;
}

享受....