简体   繁体   中英

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. I need that arraylist data to make graph in my GraphActivity.

here is my RetrieveActivity.java code

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.

public class GraphActivity extends Activity 
{

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


}

tq in advance.

In your RetrieveActivity class, do this:

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:

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

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

In you GraphActivity you can use

ArrayList newList1 = RetrieveActivity.tempList;`

late answer, but good Declare an object that implements Parcelable, implement all methods. 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.

then create intent and Bundle, and put this Arraylist in it

          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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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