0

So the case is i want to send data from one activity to another with the help of ArrayList of objects. Below is the class code which i want to use as object. So please tell me that where i am going wrong.

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

}

4

3 回答 3

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

享受....

于 2015-03-03T11:15:19.187 回答
0

第一项活动

        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");
于 2013-12-05T11:45:54.973 回答
0

试试这个,把你的arraylist使用putStringArrayListExtra

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

并获取arraylist。

Intent i = getIntent();
list = i.getIntegerArrayListExtra("arraylist");
于 2013-03-06T09:15:54.227 回答