简体   繁体   English

如何从另一个活动获取 ArrayList 到另一个

[英]How to get ArrayList from another activity to Another

I have an arraylist in my RetrieveActivity.java and i would like to transfer it to another activity which is GraphActivity.java.我的 RetrieveActivity.java 中有一个数组列表,我想将它转移到另一个活动,即 GraphActivity.java。 I need that arraylist data to make graph in my GraphActivity.我需要该数组列表数据在我的 GraphActivity 中制作图形。

here is my RetrieveActivity.java code这是我的 RetrieveActivity.java 代码

public class RetrieveActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
    }
private class MyAsyncTask extends AsyncTask<String, Void, String>
{   



    public ArrayList<String> tempList=new ArrayList<String>();
    public ArrayList<String> dataList=new ArrayList<String>();
    public ArrayList<String> atList=new ArrayList<String>();
    public ArrayList<String> timeList=new ArrayList<String>();
}
}

so I would like to get the arraylist data to the GraphActivity.所以我想将数组列表数据发送到 GraphActivity。

public class GraphActivity extends Activity 
{

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


}

tq in advance.提前tq。

In your RetrieveActivity class, do this:在您的 RetrieveActivity 类中,执行以下操作:

Intent intent = new Intent(RetrieveActivity.this, GraphActivity.class);
intent.putStringArrayListExtra("list1", mArrayList1);
intent.putStringArrayListExtra("list2", mArrayList2);
intent.putStringArrayListExtra("list3", mArrayList3);
startActivity(intent);

And then in your GraphActivity, do this to retrieve the list:然后在您的 GraphActivity 中,执行此操作以检索列表:

Intent i = getIntent();  
ArrayList<String> newList1 = i.getStringArrayListExtra("list1");
ArrayList<String> newList2 = i.getStringArrayListExtra("list2");
ArrayList<String> newList3 = i.getStringArrayListExtra("list3");

You can also declare the ArrayLists as您还可以将 ArrayLists 声明为

public static ArrayList<String> tempList=new ArrayList<String>();

In you GraphActivity you can use在你 GraphActivity 你可以使用

ArrayList newList1 = RetrieveActivity.tempList;` ArrayList newList1 = RetrieveActivity.tempList;`

late answer, but good Declare an object that implements Parcelable, implement all methods.迟到的答案,但很好 声明一个实现 Parcelable 的对象,实现所有方法。 Watch and be careful when you write read and write methods, all variables must be in the same way.在编写读写方法时要注意并小心,所有变量必须以相同的方式。 Then fill this object with data, and add this object to ArrayList that is of type object this object that you create previously.然后用数据填充这个对象,并将这个对象添加到你之前创建的对象这个对象类型的 ArrayList 中。

then create intent and Bundle, and put this Arraylist in it然后创建intent和Bundle,并将这个Arraylist放入其中

          Intent intentTest = new Intent(MainActivity.this, Calculation.class);
            data.putParcelableArrayList("test",  Razlike);
            intentTest.putExtra("dataS", data);
            //Log.i("intentCheck",String.valueOf(intentTest.getParcelableArrayListExtra("test").size()));
            startActivity(intentTest);

in other activity read that, and that is all在其他活动中阅读,仅此而已

happy coding快乐编码

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM