0

我有一个类型NetworkListarraylist的类string。我在其中使用了 parcelable 接口。但我只得到了arraylist新活动中的第一个元素。我怎样才能得到所有的元素?这是 NetworkList 类

public class NetworkList implements Parcelable{
private ArrayList<String> name=new ArrayList<String>();
private ArrayList<String> id=new ArrayList<String>();



@SuppressWarnings("unchecked")
public NetworkList(Parcel in) {
    // TODO Auto-generated constructor stub
    name=in.readArrayList(null);
    id=in.readArrayList(null);
}



public NetworkList() {
    // TODO Auto-generated constructor stub
}



public void setName(String tempValue) {
    // TODO Auto-generated method stub
    this.name.add(tempValue);
}



public void setid(String tempValue) {
    // TODO Auto-generated method stub
    this.id.add(tempValue);
}



@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeList(name);
    dest.writeList(id);
}



public ArrayList<String> getname() {
    return name;
}

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

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

};
}

这是我通过它的方式

Intent(AllNetworkActivity.this,WorkActivity.class);
            intent.putExtra("name",network);
            startActivity(intent);
    }});



}

这是我检索它的方法

   Bundle extras=getIntent().getExtras();
    NetworkList network=(NetworkList)extras.getParcelable("name");
    String name[]=new String[network.getname().size()];
    for(int i=0;i<network.getname().size();i++){
        name[i]=network.getname().get(i);
    }
    setListAdapter(new ArrayAdapter<String>(this,R.layout.work,name));

希望你能帮助我知道

4

3 回答 3

0

尝试使用适当的方法进行序列化List<String>

public NetworkList(Parcel in) {
    in.readStringList(name);
    in.readStringList(id);
}


public void writeToParcel(Parcel dest, int flags) {
    // log the number of elements - check that this is actually what you expect it to be
    // use only during development
    Log.i("NetworkList"," number of elements = "+name.size())

    dest.writeStringList(name);
    dest.writeStringList(id);
}
于 2011-06-26T08:04:06.253 回答
0

第一的

Intent Jan = new Intent(Months.this,Holiday.class);   
                    Jan.putExtra("ListCount", "Jan");   
                  startActivity(Jan);  

然后

Bundle extras = getIntent().getExtras();
        String data = extras.getString("ListCount");

        if(data.equals("Jan")){
            TextView txtMonth = (TextView)findViewById(R.id.txtMonth);
            txtMonth.setText("January");

            TextView txtDetail = (TextView)findViewById(R.id.txtDetail);
            txtDetail.setText(R.string.JanHol);
        }

检查我写的这篇文章。希望这会帮助你。

于 2011-06-26T09:30:16.960 回答
0

您还应该查看 Android 中的 Application 类。您可以在其中定义所有活动都可以访问的全局变量。然而,Bundle 是在活动之间发送数据的最流行和正确的方法。

于 2011-06-27T08:11:07.643 回答