Passing a List to another Activity in Android

后端 未结 3 539
生来不讨喜
生来不讨喜 2020-12-01 16:47

I\'ve created a list and would like to pass the list to another activity but i\'m getting an error on the putExtra statement when i create the intent. Just wondering is ther

3条回答
  •  再見小時候
    2020-12-01 17:44

    You can use putStringArrayListExtra from Intent

    public Intent putStringArrayListExtra (String name, ArrayList value)

      private final List selItemList;
      private ListView mainListView = null;       
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.recipes);
            Button searchBtn = (Button) findViewById(R.id.searchButton);
            searchBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (selItemList == null) {
                    Toast.makeText(Recipes2.this," Please Make A Selection ", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(Recipes2.this, XMLParser.class);
                    intent.putStringArrayListExtra("items_to_parse", (ArrayList) selItemList);
                    startActivityForResult(intent, 0);              
                }
            }
            });
    

    And in your XMLParser.class:

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (getIntent().getExtras() != null) {
                for(String a : getIntent().getExtras().getStringArrayList("items_to_parse")) {
                    Log.d("=======","Data " + a);
                }
            }
    

提交回复
热议问题