在Android Studio中将内容值从一个活动传递到另一个活动

时间:2016-04-04 06:17:34

标签: android-studio

活性1

private Cursor model = null;
private ClientAdapter adapter = null;
private ClientHelper helper = null;
private SharedPreferences prefs = null;
private ArrayAdapter<String> adapters;
private ArrayAdapter<String> adaptera;
private String[] available_locations;
private String[] selected_locations;
private ListView list1;
private ListView list2;

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

    list1 = (ListView) findViewById(R.id.available_locations);
    list2 = (ListView) findViewById(R.id.selected_locations);
    available_locations = getIntent().getStringExtra("List");
    .....

活动2

....
public String getID(Cursor c) {
    return (c.getString(0));
}

public String getclientName(Cursor c) {
    return (c.getString(1));
}

public String getAddress(Cursor c) {
    return (c.getString(2));
}

public String getTelephone(Cursor c) {
    return (c.getString(3));
}

public String getCuisineType(Cursor c) {
    return (c.getString(4));
}

public double getLatitude(Cursor c) {
    return (c.getDouble(5));
}

public double getLongitude(Cursor c) {
    return (c.getDouble(6));
}

public ArrayList<String> getclient;
getclient.add("clientName");
getclient.add("Address");
getclient.add("Telephone");
getclient.add("cuisineType");
getclient.add("lat");
getclient.add("lon");
public Intent intenti;
intenti = new Intent(ClientHelper.this, SetDestination.class);
intenti.putExtra("List", getclient);
startactivity(intenti);

如何将信息从Activity2传递到Activity1?

我想做一个Listview,我可以从我已经添加的列表中选择客户端(因此在activity1中有两个活动,list1和list2)

1 个答案:

答案 0 :(得分:0)

使用可以使用Bundle将数据从一个活动传递给另一个活动。

首先让对象变成parcelable。你可以使用android studio中的Parcelable插件来做到这一点。

实施例。         Intent intent = new Intent(getActivity(),targetclassname.class);         HomeScreenData homeScreenData = new HomeScreenData(); // Pojo类         intent.putExtra(&#34; categorydesc&#34;,homeScreenData);        startActivity(意向);

{
//Pojo Class`enter code here`
public class HomeScreenData implements Parcelable {
private String imagePath;
private String imageTitle;

public String getImagePath() {
    return imagePath;
}

public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}

public String getImageTitle() {
    return imageTitle;
}

public void setImageTitle(String imageTitle) {
    this.imageTitle = imageTitle;
}


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.imagePath);
    dest.writeString(this.imageTitle);
}

public HomeScreenData() {
}

protected HomeScreenData(Parcel in) {
    this.imagePath = in.readString();
    this.imageTitle = in.readString();
}

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

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

}