简体   繁体   English

如何在单独的活动中从一个活动修改ArrayList

[英]How do i modify an ArrayList from one Activity in a separate Activity

How do i add more Cars to my ArrayList<Car> from a separate Activity ? 如何从单独的Activity向我的ArrayList<Car>添加更多Cars

Because I have something like this : 因为我有这样的事情:

 private List<Car> getData() {

 List<Car> cars = new ArrayList<Car>();

cars.add(new Car("Dodge", "Viper"));

cars.add(new Car("Chevrolet", "Corvette"));

cars.add(new Car("Aston Martin", "Vanquish"));

cars.add(new Car("Lamborghini", "Diablo"));

cars.add(new Car("Ford", "Pinto"));

return cars;

}

But cars are only example, I have textswitcher in one application and I want to get text from textswitcher and place it as new item in other Activity with listview. 但是汽车只是一个例子,我在一个应用程序中有textswitcher,我想从textswitcher中获取文本,并将其作为新项目放在带有listview的其他Activity中。

Just make your List a class variable and have an addCar function 只需将List设为一个类变量并具有addCar函数

public void addCar(String make,String model){
      cars.add(new Car(make,model));
}

and change getData() 并更改getData()

    private List<Car> getData() {

            addCar("Dodge", "Viper");
            addCar("Chevrolet", "Corvette");
            etc...

        return cars;

    }

and in your other Activity just call addCar(make,model) 在您的其他Activity中,只需调用addCar(make,model)

Put cars into intent, which starts second Activity with putExtra() . cars放到意图中,这将通过putExtra()开始第二个Activity Then retrieve it with getExtra() and add the cars you want. 然后使用getExtra()检索它并添加所需的汽车。

There is no guarantee that the other Activity will exist in memory once you leave it (as the system may destroy it if memory resources become low). 无法保证其他Activity一旦离开就将存在于内存中(因为如果内存资源变少,系统可能会销毁它)。 You should save your data to persistent storage (ie a database or SharedPreferences ) or pass the data as an Intent extra instead. 您应该将数据保存到持久性存储(即数据库或SharedPreferences ),或者将数据作为Intent附加项传递。

Basically you are going to need to pass the object to the other Activity for modification using Intents . 基本上,您将需要使用Intents将对象传递给另一个Activity进行修改。 HOWEVER , this seems very wrong . 但是 ,这似乎非常错误 I would suggest re-thinking how you are adding Cars to your ArrayList outside of the Activity . 我建议您重新考虑如何将Cars添加到Activity之外的ArrayList中。

As Harmeet Singh mentioned, you might want to look into creating a sqlite table that will hold all your Cars . 正如Harmeet Singh所述,您可能需要研究创建一个可容纳所有Carssqlite表。 Then you can have your ListView have a CursorAdapter that is looking at that table. 然后,您可以让ListView具有正在查看该表的CursorAdapter This will allow you add Cars from anywhere and will be MUCH more flexible and better practice. 这将使您可以从任何地方添加Cars ,并且将更加灵活和更好地实践。

Here are two posts that discuss passing the objects as Intents to your other Activity BUT again, i do not suggest you continue down this road to implement what you are asking. 这是两篇讨论如何将对象作为Intents再次传递到另一个Activity BUT的文章,我不建议您继续沿着这条路实现您所要的内容。 Go with sqlite and a database table. 使用sqlite和数据库表。

  1. How to pass object from one activity to another in Android 如何在Android中将对象从一个活动传递到另一个活动
  2. How do I pass data between activities in Android? 如何在Android中的活动之间传递数据?

I would suggest you should use database like sqllite or to share a common resource like a xml file or any for permanent storage , this is not the way to make storage, as this type of storage is volatile . 我建议您应该使用sqllite 数据库,或者共享一个xml文件之类的公共资源或用于永久存储的任何资源 ,这不是进行存储的方法,因为这种类型的存储是易变的

All your data will go clean if memory clears in any case. 如果在任何情况下清除内存,所有数据将变得干净

have a look android persistent storage 看看android持久存储

我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity 如何将ArrayList中的类从一个活动传递给另一个活动 - How to pass Class of Class in ArrayList from one Activity to Another Activity 如何从 Settings Activity 修改 MainActivity 上的数据? - How do i modify data on my MainActivity from Settings Activity? 当我从另一个活动中删除元素时,如何清除一个活动的arraylist? - How to clear the arraylist of one activity when i delete the elements from another activity? 如何修改另一个活动中声明的ArrayList? - How to modify ArrayList declared in another activity? 如何在Android中将数据从一项活动转移到另一项活动? - How do I transfer data from one activity to another in Android? 如何将下载的图像从一个活动传递到下一个活动? - How do I pass a downloaded image from one activity to the next? 如何从另一个类调用活动的一种方法 - How do I call one method of an activity from another class 如何为多个活动恢复屏幕活动? - How do I resume screen activity for more than one activity? 如何将字符串Arraylist从一个活动传递到另一个活动? - How to pass a string Arraylist from one activity to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM