簡體   English   中英

如何在兩個活動之間傳遞數據

[英]How to pass data between two activities

我有一個NetworkList類,其中有一個string類型的arraylist 我在其中使用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));

希望你能幫助我知道

嘗試使用適當的方法來序列化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);
}

第一

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

檢查我寫的這篇文章 希望這會幫助你。

您還應該查看Android中的Application類。 您可以在其中定義全局變量,所有活動都可以訪問該變量。 但是,捆綁包是在活動之間發送數據的最流行,最正確的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM